This topic contains 7 replies, has 0 voices, and was last updated by CREECE 7 years, 7 months ago.

  • Author
    Posts
  • #1361

    CREECE

    When retrieving script parameters of type Date (runtime.getCurrentScript().getParameter), I would expect either a Date object or the String value (both datatypes it says it can return). For example, if I have 11/29/2016, I would expect either a raw Date object or ”11/29/2016″. However, when I read a Date parameter, I am getting “2016-11-29T08:00:00.000Z” which is a string type. If I try to parse this with the format module, I am expecting a raw date object but instead, I get “2016-11-29T08:00:00.000Z” which is the same string I fed to it. Even passing in just “2016-11-29” returns the same string. This format of YYYY-MM-DD isn’t one that my user has specified so I am not sure where it is coming from. Are date params like this ALWAYS returned with the YYYY-MM-DD format? I need to know the format so I can create the correct date object as something like 2016-08-07 could be August 7, 2016 or July 8th 2016 depending on the format.

    YYYY-MM-DD is not my user preference OR what the company preference is set to. Both are set to MM/DD/YYYY.

    I want to work with raw javascript date objects so that I can use momentjs to do date manipulation.

    Console example with the script parameter I am getting in my server-side script:

    Code:
    require([‘N/format’], function(format) {

    var date = format.parse({value: ‘2016-11-29T08:00:00.000Z’, type: format.Type.DATE});

    console.log(‘DOES NOT WORK –> date: ‘ + date + ‘ typeof: ‘ + typeof(date));

    date = format.format({value: ‘2016-11-29’, type: format.Type.DATE});

    console.log(‘DOES NOT WORK –> date: ‘ + date + ‘ typeof: ‘ + typeof(date));

    date = format.parse({value: ’11/29/2016’, type: format.Type.DATE});

    console.log(‘WORKS –> date: ‘ + date + ‘ typeof: ‘ + typeof(date));
    });
    Thanks!
    This is a cached copy. Click here to see the original post.

  • #1362

    rmlakhani

    Hi Chris,

    I am receiving the same error as you. I have filed a defect with NS. Defect Number 427585. It seems as though the format.format function is not working as described where it takes in a raw value of type (String, Date, Number) and converts to a value that is set from the Setup > Company > General Preference. In the debugger it returns the values that you have described.

    I hope this issue gets resolved quickly.

    Regards,

    Rehan

    Audaxium Developer.

  • #1363

    CREECE

    Thanks for the update GhostDZ9

    It seems that the raw value returned when reading the script parameter isn’t a recognized date object at all which it should be. Format seems to work correctly when provided a valid date string or object. Also the user preference for date format should trump the company general one so the company defined one isn’t always used I believe. Either way, I too hope this is resolved quickly as it is blocking movement.

    I do not have access to account center, do you know what status this defect is in?


    rnedelkow replied on 01/03/2017, 10:38 AM: Defect 427585 is S2 severity and no eta is available at this time.

  • #1364

    CREECE

    Anyway to get this to an S1? This is definitely impacting core business functionality and is at least multiple customer.


    rnedelkow replied on 01/03/2017, 11:12 AM: Please contact Support and provide your details along with your use case.

  • #1365

    ironside

    That looks like ISO8601 format. If you use momentjs it should parse that string by default.


    CREECE replied on 01/05/2017, 07:08 PM: Yeah it does appear to be ISO8601 (YYYY-MM-DD) and moment will definitely parse this (even if you do/do not need to include the format string depending on your moment version). The main issue here is that format module cannot parse this and also is this even what the field type is supposed to be returning. Actual functionality vs what is stated in 2.0 docs doesn’t line up for format.format module and possibly the type returned from reading a date type field via 2.0.

  • #1366

    rmlakhani

    Hi Chris,

    I have figured out the issue and the workaround.

    A scheduled script when executing uses the formatting specified at the company level. If the date you are trying to parse/format is a different format when executing the script you will get an unexpected error.

    To resolve this use the following:

    Code:
    //GENERATE JAVASCRIPT DATE OBJECT
    var date = new Date();

    //NOT NECESSARY ONLY IF YOU NEED TO PASS HOURS AND MINUTES
    var executionDate = ‘ ‘ + date.getHours() + ‘:’ + date.getMinutes();

    var executionDT = format.format({
    value : date,
    type : format.Type.DATE
    });

    executionDT += executionDate;
    executionDT will return today’s date in the company level formatting.

    If you are testing this in the debugger simply adjust your user level preference to match the company level preference.

    Regards,

    Rehan

    Audaxium Developer


    CREECE replied on 01/09/2017, 02:27 PM: This is not a valid workaround as this doesn’t address the issue. My company level and user level preference (which overrides company level) are not in this format. Your example works because you are not reading a script parameter, you are creating a date object which format.format understands and formats accordingly. If you read a date object and it is returned in the ISO8601 format, format.format cannot do anything with it. The only workaround i’ve seen so far is to create a moment with the string and ‘YYYY-MM-DD’ as the 2nd parameter and then run toDate() on the moment. This will then give you a valid date object in the format you are looking for when running it through format.format.

  • #1367

    JayDP123

    If the parameter is giving you 2016-11-29T08:00:00.000Z ; if you run util.isDate() on that does it return true or false?

    If it is false then why can’t you use substr() to take the day,month,year or whatever you need and create a raw javascript Date object with that? With that javascript Date object you should then be able to use moment.js with it no?

    require([‘N/log’,’N/format’],function(log,format){

    paramDate = “2016-11-29T08:00:00.000Z”;

    log.debug(util.isDate(paramDate));

    year = paramDate.substr(0,4);

    month = paramDate.substr(5,2);

    day = paramDate.substr(8,2);

    var jsDate = new Date(year,month,day);

    log.debug(util.isDate(jsDate));

    });


    CREECE replied on 01/10/2017, 03:21 PM: This won’t work as you don’t know which one is the month and which one is the day until you get the format.

  • #1368

    CREECE

    It looks like the map/reduce scripts are still returning the date parameters as strings and other scripts like scheduled script is returning it as a date object. Has there been any movement on this defect (Defect 427585 is S2 severity)? I do not have any access to look at defects.

You must be logged in to reply to this topic.