0

I am having issues in comparing date formats: 05/31/2017 10:50 AM (IST) and 20170531 003837.000(EST) using Date.parse. Any leads on this?.

//Capture input for debug
var Outlmd = "05/31/2017 10:50 AM";
var Outlsr = "20170531 003837.000";
//Convert to internal format - milliseconds since epoch
d1 = Date.parse(05/31/2017 10:50 AM);
d2 = Date.parse(20170531 003837.000);

if(d1 > d2) { NewTempDate = lmd; } else { NewTempDate = lsr; }
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • //Capture input for debug var Outlmd = "05/31/2017 10:50 AM"; var Outlsr = "20170531 003837.000"; //Convert to internal format - milliseconds since epoch d1 = Date.parse(05/31/2017 10:50 AM); d2 = Date.parse(20170531 003837.000); if(d1 > d2) { NewTempDate = lmd; } else { NewTempDate = lsr; –  May 31 '17 at 05:11
  • 1
    What are the issues you are facing? Add the code in your question not as a comment – Colwin May 31 '17 at 05:12
  • 1
    https://momentjs.com/ – Phil May 31 '17 at 05:13
  • Hi Phil I want to pass the dates as arguments as follows: //Capture input for debug var lmd = "05/31/2017 10:50 AM"; var lsr = "20170531 003837.000"; //Convert to internal format - milliseconds since epoch d1 = Date.parse(lmd); d2 = Date.parse(lsr); if(d1 > d2) { NewTempDate = lmd; } else { NewTempDate = lsr; } –  May 31 '17 at 05:19
  • Hi Colwin, Phil....Here is the code: //Capture input for debug var lmd = "05/31/2017 10:50 AM"; var lsr = " 2017-05-31T00:45:25-0400"; //Convert to internal format - milliseconds since epoch d1 = Date.parse(05/31/2017 10:50 AM); d2 = Date.parse(20170531 003837.000); if(d1 > d2) { NewTempDate = lmd; } else { NewTempDate = lsr; } –  May 31 '17 at 05:30
  • 1
    code belongs in the question – Jaromanda X May 31 '17 at 05:30
  • Rob What would be the valid code to compare the above formats then –  May 31 '17 at 05:55

1 Answers1

-1

You forgot quotes within Date.parse

Do Date.parse('05/31/2017 10:50 AM')

Update Please consider the code below:

//Capture input for debug
var lmd = "05/31/2017 10:50 AM";
var lsr = "2017-05-31T00:45:25-0400";
//Convert to internal format - milliseconds since epoch
d1 = Date.parse(lmd);
d2 = Date.parse(lsr);
if(d1 > d2) { NewTempDate = lmd; } else { NewTempDate = lsr; }

Please note that lmd and lsr should be parsable data string without extra spaces: "2017-05-31T00:45:25-0400" not " 2017-05-31T00:45:25-0400"

Kosh
  • 16,966
  • 2
  • 19
  • 34
  • //Capture input for debug var Outlmd = "05/31/2017 10:50 AM"; var Outlsr = "20170531 003837.000"; //Convert to internal format - milliseconds since epoch d1 = Date.parse(05/31/2017 10:50 AM); d2 = Date.parse(20170531 003837.000); if(d1 > d2) { NewTempDate = lmd; } else { NewTempDate = lsr; } –  May 31 '17 at 05:19
  • Hi Kosh I want to pass the dates as arguments as follows: //Capture input for debug var lmd = "05/31/2017 10:50 AM"; var lsr = "20170531 003837.000"; //Convert to internal format - milliseconds since epoch d1 = Date.parse(lmd); d2 = Date.parse(lsr); if(d1 > d2) { NewTempDate = lmd; } else { NewTempDate = lsr; } –  May 31 '17 at 05:21
  • That's OK. But `20170531 003837.000` is not a valid date format. It's not parsable. – Kosh May 31 '17 at 05:22
  • How about 2017-05-31T00:45:25-0400?? –  May 31 '17 at 05:27
  • How about 2017-05-31T00:45:25-0400? –  May 31 '17 at 05:28
  • 2017-05-31T00:45:25-0400 is parsable – Kosh May 31 '17 at 05:29
  • see update in my answer – Kosh May 31 '17 at 05:40
  • You should not depend on *Date.parse* to parse non-standard strings. Even standards compliant strings are not parsed correctly by browsers in use. – RobG May 31 '17 at 05:41
  • but kosh I am not able to compare the above 2 formats –  May 31 '17 at 05:42
  • `surya amala Vungarala` what do you mean? – Kosh May 31 '17 at 05:47
  • It does not result in valid results when compared after date parsing.Could you suggest the valid code –  May 31 '17 at 05:57
  • so which results are valid? – Kosh May 31 '17 at 06:00