Linda Linda - 1 year ago 58
Javascript Question

JavaScript date objects UK dates

I have the following code

dateSet = datePicker.val();
dateMinimum = dateChange();
dateSetD = new Date(dateSet);
dateMinimumD = new Date(dateMinimum);
alert('You can not amend down due dates');

dateSet = "01/07/2010"
dateMinimum = "23/7/2010"

Both are UK format. When the date objects are compared dateSetD should be less than dateMinimumD but it is not. I think it is to do with the facts I am using UK dates dd/mm/yyyy. What would I need to change to get this working?

Answer Source

The JavaScript Date constructor doesn't parse strings in that form (whether in UK or U.S. format). See the spec for details, but you can construct the dates part by part:

new Date(year, month, day);

MomentJS might be useful for dealing with dates flexibly. (This answer previously linked to this lib, but it's not been maintained in a long time.)