0

I want to turn the string 1822-01-01 00:00:00 into a date by:

var d= new Date("1822-01-01 00:00:00");

What I expect using d.toLocaleString() is 1.1.1822, 00:00:00, but what I get is 31.12.1821, 23:53:28.

See fiddle: https://jsfiddle.net/f1kLpfgd/

Javascript Date string constructing wrong date explains the wrong date with time zones. I have a different problem, as even minutes and seconds differ from my input. The solution using the new Date(1822, 0, 1, 0, 0, 0, 0) constructor does not work for me, as the result is 31.12.1821, 23:53:28.

Is it because the year is before 1901? But even 1899 works perfectly fine...

Update

For the formatting example custom VS toLocaleString, see updated fiddle: https://jsfiddle.net/f1kLpfgd/8/

Neepsnikeep
  • 309
  • 3
  • 11
  • Possible duplicate of [Javascript: parse a string to Date as LOCAL time zone](https://stackoverflow.com/questions/33908299/javascript-parse-a-string-to-date-as-local-time-zone) – Brahma Dev Sep 20 '17 at 15:15
  • 1
    Month 1 is **February** in JavaScript. – Pointy Sep 20 '17 at 15:18
  • 4
    use `d.toString()` instead of `d.toLocaleString()` because toLocaleString dependent on current time zone. [https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date/toLocaleString](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date/toLocaleString) – chakradhar kasturi Sep 20 '17 at 15:22
  • @BrahmaDev: As I pointed out, I'd understand if only the hours differed. But time zones don't explain second differences. – Neepsnikeep Sep 20 '17 at 16:03
  • @Pointy: A yes thank you. I updated that part. The time is still different, though. – Neepsnikeep Sep 20 '17 at 16:03
  • @chakri: Huh... that... seems to be the wanted date. Thanks! So it's only formatted horribly wrong... I still have trouble formatting the date the way I want, but your hint tells me the date itself should be correct. – Neepsnikeep Sep 20 '17 at 16:03
  • 1
    @Neepsnikeep I realize it now. The change appears in 1911/12. Don't know why. – Brahma Dev Sep 20 '17 at 16:12
  • Don't use the built-in parser. In Safari, `new Date("1822-01-01 00:00:00")` returns an invalid date. See [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) Also, the format returned by *toLocaleString* is implementation dependent, even the related standard (ECMA 402) does not define the format to use for particular languages (it's not actually based on locale at all). – RobG Sep 20 '17 at 23:44

2 Answers2

1

If you don't want to use some library like date-fns , moment.js ...
To get the desired you could try like:

var dateString = "1822-01-01 00:00:00";

var d = new Date(dateString);
var formatted = d.getDate() +'.'+
                (d.getMonth()+1) +'.'+
                d.getFullYear() +', '+
                dateString.substr(11);

console.log(formatted);
Roko C. Buljan
  • 196,159
  • 39
  • 305
  • 313
  • That works and moments.js does exactly what I want, thanks. https://jsfiddle.net/f1kLpfgd/7/ – Neepsnikeep Sep 20 '17 at 20:13
  • Please do not suggest using the built-in parser, particularly for non–standard strings. In Safari, `new Date("1822-01-01 00:00:00")` returns an invalid date. – RobG Sep 20 '17 at 23:42
  • @Roko: Ah, now I see why you put the month+1 in parentheses. Thanks for solving a problem I haven't run into, saved me a lot of time. – Neepsnikeep Sep 27 '17 at 06:33
1

I want to turn the string 1822-01-01 00:00:00 into a date by:

var d= new Date("1822-01-01 00:00:00");

What I expect using d.toLocaleString() is 1.1.1822, 00:00:00, but what I get is 31.12.1821, 23:53:28.

Do not use the built-in parser for non-standard strings as whatever you get is implementation dependent and likely not what you expect in at least some hosts. In Safari, d will be an invalid date.

The format returned by toLocaleString is implementation dependent and varies between browsers. For me, new Date().toLocaleString() returns "9/21/2017, 9:48:49 AM", which is not consistent with the format typically used either in my locality or by users of the language I speak.

If you just want to reformat the string, see Reformat string containing date with Javascript.

If you want to know how to parse the string correctly, see Why does Date.parse give incorrect results?

If you want to format a Date, see Where can I find documentation on formatting a date in JavaScript?

RobG
  • 142,382
  • 31
  • 172
  • 209
  • Thanks for the hint, I will test with Safari. I will not write my own parser for stuff whole libraries are dedicated to. For the locations I need, toLocaleString gives me the right format (just with wrong numbers). That's why I wanted to use it rather than do some unreadable custom formatting in that line. But as it's the only way I get a correct date string, I do it now. – Neepsnikeep Sep 27 '17 at 06:17
  • @Neepsnikeep—the "wrong numbers" appears to be a bug, most browsers have some for particular times or dates (testing around daylight saving changeover is fraught). – RobG Sep 28 '17 at 06:36