0

When using the below line I am expecting the result to be '22/09/2017 14:05:43', however it is actually returning '09‎/‎10‎/‎2018‎ ‎14‎:‎05‎:‎43'.

var theDate = new Date('22/09/2017 14:05:43').toLocaleString();

I know there are js libraries out there such as moment.js that can be used for a lot of date time manipulation, but I was just wondering if anyone knew why this was happening and how I can get this to return the expected date?

GradviusMars
  • 71
  • 1
  • 1
  • 7
  • 2
    JavaScript dates are in MM/DD/YYYY format, not DD/MM/YYYY format. – James Donnelly Oct 18 '17 at 10:47
  • 1
    `Date` format should conform to standard supported by `Date.parse` https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse – gurvinder372 Oct 18 '17 at 10:48
  • 2
    When I try your code, I just get 'invalid date'. Surprised you don't. – putvande Oct 18 '17 at 10:48
  • 2
    It's either MM/DD/YYYY or the ISO's YYYY-MM-DD, as per https://www.w3schools.com/js/js_date_formats.asp. I wonder why people insist on the MM/DD/YYYY when the ISO's YYYY-MM-DD is considered the international standard. – Wiktor Zychla Oct 18 '17 at 10:51

2 Answers2

3

Parsing of date strings is mostly implementation dependent, so it's generally recommended to avoid the built-in parser. Most browsers treat JavaScript dates with a pattern xx/xx/xxxx as MM/DD/YYYY, so 22/09/2017 is seen as either an invalid date (e.g. Safari, Firefox, Chrome), or the 9th day of the 22nd month of 2017 (apparently your browser).

Your browser is interpreting it as 'the 9th day of the 22nd month', so you're ending up on October 9th 2018, 22 months and 9 days into 2017.

To resolve this, you can separate the string into parts and give them to the constructor, avoiding the built-in parser (remembering to subtract 1 from the month number):

new Date(2017, 8, 22, ...)

Refer to MDN's Date documentation for more information.

RobG
  • 142,382
  • 31
  • 172
  • 209
James Donnelly
  • 126,410
  • 34
  • 208
  • 218
  • 1
    Not mentioning the ISO 8601 format while providing alternatives ("or separate your Date constructor [...]") could make people coming here from search engines believe that there is no other way. I believe the ISO deserves at least mentioning it in an answer that has been accepted. Also, https://xkcd.com/1179/ – Wiktor Zychla Oct 18 '17 at 11:23
  • @RobG I've revised the answer. Thanks! – James Donnelly Oct 19 '17 at 09:11
0

Well the initial date string you passed to the Date constructor is wrong, it should be in the format 'MM/DD/YYYY HH:mm:ss'.

Your actual code will give Invalid Date:

var theDate = new Date('22/09/2017 14:05:43');
console.log(theDate);
var str = theDate.toLocaleString();
console.log(str);

Here 22 will be treated as a month and 09 will be treated as day. You need to fix it so it follows the Date standards:

var theDate = new Date('09/22/2017 14:05:43');
console.log(theDate);
var str = theDate.toLocaleString();
console.log(str);
cнŝdk
  • 31,391
  • 7
  • 56
  • 78
  • The issue is that the OP should not use the built-in parser for non-standard strings. – RobG Oct 19 '17 at 08:44