What is the Syntax for inserting a Javascript Date into a Search in SuiteScript?

var start = new Date(2019, 0, 1);
var end = new Date(2019, 0, 31);
var itemsSearch = searchModule.create({
    type: 'salesorder',
    filters:
    [
        ['type', 'anyof', 'SalesOrd'],
        'AND',
        ['datecreated', 'within', start, end],
    ],
    columns:
    [....]
});

I am trying to add dynamic dates to a Search. Is this the correct Syntax for adding a javascript Date as a parameter to the search?

Beginner Asked on September 11, 2019 in SuiteScript.
Add Comment
2 Answer(s)
Best answer

More specifically, they need to be in the format specified by the preferences of the user running the script. After you’ve created your Date objects, you can use N/format.format() to output them as strings in the correct format.

If you don’t need them as Dates for further calculation or anything, you could just format them directly.

Should end up looking something like:


var startStr = format.format({type: format.Type.DATE, value: new Date(...)});
var endStr = format.format({type: format.Type.DATE, value: new Date(...)});;
...
filters: [['datecreated', search.Operator.WITHIN, startStr, endStr]]
...

Beginner Answered on September 11, 2019.

Cheers bro in a land not far away you are known as the Stoic Beast of Netsuite development. Thanks, man your tutorials are the best. Without your explanations, I would have long given up on Netsuite. You never disappoint.

on October 8, 2019.

How about filtering by date and time.  If I change the code above to format.Type.DATETIME or format.Type.DATETIMETZ then I get UNEXPECTED_ERROR.

on March 30, 2021.
Add Comment

Dates in filters need to be strings in ‘D/M/YYYY’ format

Intermediate Answered on September 11, 2019.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.
  • This site made possible by our sponsors:   Tipalti   Celigo   Limebox   Become a Sponsor   Become a Sponsor