# Search on date smart field

**URL:** <https://community.forestadmin.com/t/search-on-date-smart-field/1613>\
**Category:** Help me!\
**Created:** [January 13, 2021, 1:56pm UTC](https://community.forestadmin.com/t/search-on-date-smart-field/1613 "2021-01-13T13:56:19Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![daweido](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/daweido/32/1360_2.png) [@daweido](https://community.forestadmin.com/u/daweido)\
**Post date:** [January 13, 2021, 1:56pm UTC](https://community.forestadmin.com/t/search-on-date-smart-field/1613/1 "2021-01-13T13:56:19Z")

</div>

Hey everyone,

I just created a Smart field containing multiple columns. One of these columns are dates. I can’t seem to be able search dates.

## Expected behavior

Being able to search smart fields having dates

## Actual behavior

I get an error.

And this is my code:

```javascript
let s = models.sequelize;
let split = search.split(" ");

let tableDateSearchConditions = split.map((x) => {
  return {
    tableDate: { [Op.like]: `%${x}%` },
  };
});

let searchCondition = {
  [Op.or]: [].concat(tableDateSearchConditions),
};

query.where[Op.and] = query.where[Op.and].concat(searchCondition);

return query;

```

## Failure Logs

```javascript
Deprecation warning: value provided is not in a recognized RFC2822 or ISO format. moment construction falls back to js Date(), which is not reliable across all browsers and versions. Non RFC2822/ISO date formats are discouraged. Please refer to http://momentjs.com/guides/#/warnings/js-date/ for more info.
Arguments: 
[0] _isAMomentObject: true, _isUTC: false, _useUTC: false, _l: undefined, _i: %2020%, _f: undefined, _strict: undefined, _locale: [object Object]
Error

```

Would any of you know how I can query dates correctly ?

---

<div class="post-metadata">

**Author:** ![anon94532230](https://avatars.discourse-cdn.com/v4/letter/a/a587f6/32.png) [@anon94532230](https://community.forestadmin.com/u/anon94532230)\
**Post date:** [January 13, 2021, 1:58pm UTC](https://community.forestadmin.com/t/search-on-date-smart-field/1613/2 "2021-01-13T13:58:15Z")

</div>

Hi @daweido !  
Can you share the format of the dates you are trying to filter ?

---

<div class="post-metadata">

**Author:** ![daweido](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/daweido/32/1360_2.png) [@daweido](https://community.forestadmin.com/u/daweido)\
**Post date:** [January 13, 2021, 2:03pm UTC](https://community.forestadmin.com/t/search-on-date-smart-field/1613/3 "2021-01-13T14:03:00Z")

</div>

Hi @anon94532230,

Thanks for replying so quickly.

Here is my field’s get function.

```javascript
get: (table) => {
				return models.restaurants
					.findOne({
						where: { restaurantId: table.restaurantIdKey },
					})
					.then((restaurant) => {
						return `${restaurant.restaurantName} | ${moment(
							table.tableDate
						).format("L - LT")} | ${table.tableAvailableSeats}`;
					});

```

---

<div class="post-metadata">

**Author:** ![anon94532230](https://avatars.discourse-cdn.com/v4/letter/a/a587f6/32.png) [@anon94532230](https://community.forestadmin.com/u/anon94532230)\
**Post date:** [January 13, 2021, 2:16pm UTC](https://community.forestadmin.com/t/search-on-date-smart-field/1613/4 "2021-01-13T14:16:28Z")

</div>

I think it might be related to the format of the date, I’m not sure sequelize support this one 🤔  
Can you try with something like this ? `YYYY-MM-DD HH:mm:ss`

---

<div class="post-metadata">

**Author:** ![daweido](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/daweido/32/1360_2.png) [@daweido](https://community.forestadmin.com/u/daweido)\
**Post date:** [January 13, 2021, 2:21pm UTC](https://community.forestadmin.com/t/search-on-date-smart-field/1613/5 "2021-01-13T14:21:42Z")

</div>

Thank you.

The same error still occurs. Does this have to do with the fact that I am using Momentjs to format the date?

---

<div class="post-metadata">

**Author:** ![anon94532230](https://avatars.discourse-cdn.com/v4/letter/a/a587f6/32.png) [@anon94532230](https://community.forestadmin.com/u/anon94532230)\
**Post date:** [January 13, 2021, 2:31pm UTC](https://community.forestadmin.com/t/search-on-date-smart-field/1613/6 "2021-01-13T14:31:01Z")

</div>

I found this post: [https://stackoverflow.com/a/58759791/7996564](https://stackoverflow.com/a/58759791/7996564)  
It looks like your error, can you try to pass the date input format to moment ?

---

<div class="post-metadata">

**Author:** ![daweido](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/daweido/32/1360_2.png) [@daweido](https://community.forestadmin.com/u/daweido)\
**Post date:** [January 13, 2021, 3:25pm UTC](https://community.forestadmin.com/t/search-on-date-smart-field/1613/7 "2021-01-13T15:25:29Z")

</div>

Thank you @anon94532230.

I was able to implement it correctly, I just had to change the way I did my search.

Here is my code if anyone needs it:

```javascript
get: (table) => {
				return models.restaurants
					.findOne({
						where: { restaurantId: table.restaurantIdKey },
					})
					.then((restaurant) => {
						return `${restaurant.restaurantName} | ${moment(
							table.tableDate
						).format("L - LT")} | ${table.tableAvailableSeats}`;
					});
			},
			search: function (query, search) {
				query.include.push({
					model: models.restaurants,
					as: "restaurant",
				});

				let s = models.sequelize;
				let split = search.split("|");
				const searchConditions = [];
				const splitLength = split.length;

				if (splitLength >= 1 && split[0].length > 0) {
					searchConditions.push(
						s.literal(`restaurant.restaurantName LIKE '%${split[0].trim()}%'`)
					);
				}

				if (splitLength >= 2 && split[1].length > 0) {
					searchConditions.push({
						tableDate: {
							[Op.like]: `%${moment(split[1].trim(), "L - LT").format(
								"L - LT"
							)}%`,
						},
					});
				}

				if (splitLength >= 3 && split[2].length > 0) {
					searchConditions.push({
						tableAvailableSeats: { [Op.eq]: split[2].trim() },
					});
				}

				let searchCondition = {
					[Op.and]: searchConditions,
				};

				query.where[Op.and] = query.where[Op.and].concat(searchCondition);

				return query;
			},

```
