Forum Discussion

SnapWizard's avatar
SnapWizard
New Contributor III
5 years ago
Solved

Date.Parse() Function

I have an incoming file that has a date value of 00:00.00.000 as a value. I am trying to set it to null. Comparing the exit_date ==‘00:00:00.000’ fails. Checking Date.parse($exit_date)==’ ’ or Date.parse($exit_date==null fails. Date.parse($exit_date).ToLocalDateString() fails. Not sure how I can parse this data. Any ideas on how to map this value to null?

  • Try this expression in your mapper:

    isNaN(Date.parse($.get('exit_date') || '')) ? null : Date.parse($exit_date).toLocaleDateTimeString()
    

    Having not seen your entire input set, I can’t guarantee it will work for every record, but I think it will come very close, if not fully succeed.

    The idea of this expression is:

    If $exit_date exists and is translatable to dateobject, it will produce a datetime in which the LocaleDateTimeString() will result into a valid value; otherwise the result is null.

    It is dependent on the expectation that NaN is always the result from Date.parse() for non-parsable parameters.

6 Replies

  • del's avatar
    del
    Contributor III

    Try this expression in your mapper:

    isNaN(Date.parse($.get('exit_date') || '')) ? null : Date.parse($exit_date).toLocaleDateTimeString()
    

    Having not seen your entire input set, I can’t guarantee it will work for every record, but I think it will come very close, if not fully succeed.

    The idea of this expression is:

    If $exit_date exists and is translatable to dateobject, it will produce a datetime in which the LocaleDateTimeString() will result into a valid value; otherwise the result is null.

    It is dependent on the expectation that NaN is always the result from Date.parse() for non-parsable parameters.

    • SnapWizard's avatar
      SnapWizard
      New Contributor III

      Thank you. That worked.

  • The conditional operator (? : ) may help here. Is date field a string? If so, something
    like this may work. You could replace “==” comparison with a string operation (.contains, .startsWith)

      MAPPER
          Expression  : $exit_date == '00:00.00.000' ? null : $exit_date
          Target Path : $exit_date
    
  • del's avatar
    del
    Contributor III

    One thing I notice is that it seems there is a typo in the time-format for either the $exit_date value or the string-comparison expression.

    I think "00:00.00.000" should be "00:00:00.000".
    (i.e. the minute:second delimiter a colon instead of period)

    Also, Date.Parse() should be Date.parse() (lowercase ‘p’)

    In any case, if the $exit_date value contains the time-format typo, then Date.parse() probably returns a NaN value instead of null or '' (but I haven’t tested to confirm).

    • SnapWizard's avatar
      SnapWizard
      New Contributor III

      Sorry, Yes I had typos in my question: Here’s some more detail:
      I have a Mapper Snap with this logic:
      $exit_date!=’ ’ && $exit_date!=null && $exit_date!=‘00:00:00.000’?
      Date.parse($exit_date).toLocaleDateString() : null

      I am getting this error message on that snap:
      Expression worked for 1 previous document, but failed on document #2 with the error: Not-a-number

      (NaN) does not have a method named: toLocaleDateString, found in: …aleDateString(). Perhaps you meant: toString, toExponential, toPrecision, toFixed

      Resolution:
      Handle the NaN value by providing a default value, for example: ($exit_date!=‘’&&$exit_date!=null&&$exit_date!=‘00:00:00.000’)?(Date.parse($exit_date) || /* default */).toLocaleDateString():null

      Reason:
      ‘toLocaleDateString’ was not found while evaluating the sub-expression ‘Date.parse($exit_date).toLocaleDateString()’

      I tried the resolution and the same error appears

  • viktor_n's avatar
    viktor_n
    Contributor II

    Hi @SnapWizard,

    As I see this is just a time not the whole date. And when you work only with time you should use LocalTime.parse() instead of Date.parse().