0

I'm working on a JavaScript application. I have two different String dates 31/10/2013 and 1/11/2013 and I create an instance of these two dates with new Date(string).getTime();

But it shows this (the same date ) as the result:

console.log(date_s + " after new date " + date ); 31/10/2013 after new date Fri Nov 1 00:00:00 UTC 2013 1/11/2013 after new date Fri Nov 1 00:00:00 UTC 2013 
3
  • What's the point of getting the time of dates you just created with NO TIME ? Commented Oct 30, 2013 at 13:48
  • 2
    "31/10/2013" and "1/11/2013" are not valid date strings. The equivalent correct strings should be "2013/10/31" and "2013/11/1". Constructing a Date object with either of your strings results in an invalid date, and calling getTime results in NaN. Commented Oct 30, 2013 at 13:48
  • 1
    To clarify the comment from @JamesAllardice, it might work sometimes: "The function first attempts to parse the format of the String according to the rules called out in Date Time String Format (15.9.1.15). If the String does not conform to that format the function may fall back to any implementation-specific heuristics or implementation-specific date formats." That is, to clarify further: it might work for the OP, but it's not a standard universal behavior. Commented Oct 30, 2013 at 13:51

3 Answers 3

1

You haven't a valid string in you new Date(string)

Some example to initialize dates

var my_date=new Date(2013,10,31) 

and all the documentation on http://www.w3schools.com/js/js_obj_date.asp

Sign up to request clarification or add additional context in comments.

Comments

0

31/10/2013 is not a valid date string unless you've got maybe some localization going on. To the default localization settings for en-US, it should be 10/31/2013. What your string means is "month 31 of 2013" which pushes new Date('31/10/2013') to be some time in 2015 because that's where it resolves the date due to that "month 31."

Comments

0

If you want an easy solution, try moment.js - a powerful javascript date parser/formatter/validator/manipulator.

In moment, you can parse date with the syntax like this [doc]:

//this will gives you a correct date object moment('31/10/2013', 'DD/MM/YYYY').toDate(); 

Else, you can always welcome to split and rebuild the date object.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.