-2

I have a date format:

Thursday, 10 Aug 2017

I have an array of dates like this above. I have to find out the earliest date in the the array in javascript. How can I effectively parse the date in the above format to check which is the earliest date. How can I do that?

I also tried Date.parse method. Its working. Is it a good way of doing this? Can this method break or throw some exceptions? Also will this add any added latency to the code? As this method checking is alll done client side and we don't want any latency of any kind.

Any leads appreciated.

user2696258
  • 1,159
  • 2
  • 14
  • 26
  • 1
    `Java` is not `JavaScript`.... – Usagi Miyamoto Aug 01 '17 at 09:41
  • what did you try? – Slavik Aug 01 '17 at 09:43
  • I tried using var dt = new Date(date1); So dt comes = "Thu Aug 10 2017 00:00:00 GMT+0530 (IST)". dt.getDate or dt.getYear methods return empty functions and not the values. What can I be doing wrong? – user2696258 Aug 01 '17 at 09:47
  • I also tried Date.parse method. Its working. Is it a good way of doing this? Can this method break or throw some exceptions? Also will this add any added latency to the code? As this method checking is alll done client side and we don't want any latency of any kind. – user2696258 Aug 01 '17 at 09:52

2 Answers2

0

You can use momentjs and parse the date with

moment.lang("en-au").format('LLLL');

Have a look at moment

If you do not want moment just use what you say above

var d = new Date('Thursday, 10 Aug 2017')
d.getDate() --- 10
d.getFullYear() --- 2017

And if you want to find the earliest one, you can do the following(it does need refactor):

var earliest = null;
var dates = ['Thursday, 11 Aug 2017', 'Thursday, 10 Aug 2017' , 'Thursday, 15 Aug 2017'];
dates.forEach(date => {earliest = earliest == null ? new Date(date) : (new Date(date).getTime() < earliest.getTime()) ? new Date(date) : earliest});
kimy82
  • 4,069
  • 1
  • 22
  • 25
  • I don't want to add any library to my code. The way I am doing, is there any latency concerns? – user2696258 Aug 01 '17 at 10:29
  • I have seen all these answers, but still confused as they all are using Date Object. Is it safe to convert the String to Date object? Can that not throw exception or give unreliable results? What can be the most reliable way of doing this? When you say unreliable results, can this method give different/wrong values even when we are sure the the date format will not change --> It would be same as mentioned in the question(Thursday, 10 Aug 2017). Your help here is very much appreciated. – user2696258 Aug 02 '17 at 03:34
  • Can this give unreliable results for same date value on different OS/browsers/devices? – user2696258 Aug 02 '17 at 03:34
0

This is a duplicate, but you've asked many questions in one go:

How can I effectively parse the date in the above format to check which is the earliest date. How can I do that?

Duplicate of Why does Date.parse give incorrect results?

I also tried Date.parse method. Its working. Is it a good way of doing this?

No, for the reasons outlined in the duplicate: it gives unreliable results.

Can this method break or throw some exceptions?

No, but it gives unreliable results.

Also will this add any added latency to the code?

No, but it gives unreliable results.

RobG
  • 142,382
  • 31
  • 172
  • 209
  • I have seen all these answers, but still confused as they all are using Date Object. Is it safe to convert the String to Date object? Can that not throw exception or give unreliable results? What can be the most reliable way of doing this? When you say unreliable results, can this method give different/wrong values even when we are sure the the date format will not change --> It would be same as mentioned in the question(Thursday, 10 Aug 2017). Your help here is very much appreciated. – user2696258 Aug 02 '17 at 03:30
  • Can this give unreliable results for same date value on different OS/browsers/devices? – user2696258 Aug 02 '17 at 03:34
  • Please don't ask questions in comments. All your questions are answered, `new Date(string)` and `Date.parse(string)` will parse the string in exactly the same way, the only difference is one returns a Date and the other a number (which is the time value of the Date). Implementations differ: what works in one may not work in another. See the parsing duplicate. – RobG Aug 02 '17 at 05:09