0

I have date 14.05.2017 15:32 its polish format of date and time dd.MM.yyyy HH:mm when im using moment library with locales it always set time to default value of 00:00:00.

Here is example code that i've tried to use Fiddle

console.log(moment('14.05.2017 15:32').format('dd.MM.yyyy HH:mm'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.19.3/moment.min.js"></script>

Any other suggestions how to parse it? Expected result:

2017-05-14T15:32:00.004Z
RobG
  • 142,382
  • 31
  • 172
  • 209
Wojna
  • 136
  • 1
  • 17
  • Please post code here as a runnable snippet, not elsewhere. External libraries can be included. "pl" is a language code, not a format. You should not base the format on the language since there are many formats that are unrelated to language (and vice versa). You are also ignoring warnings about how you're parsing the string. See [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) – RobG Dec 06 '17 at 22:42
  • A string without a timezone will be treated as local. The only case where '14.05.2017 15:32' will convert to '2017-05-14T15:32:00.004Z' is where the host timezone offset is +0000. – RobG Dec 06 '17 at 22:48
  • Why did not you accept my answer? – kiks73 Mar 07 '18 at 14:43

1 Answers1

0

You should replace the 'pl' argument with the explicit format to parse and add the argument to the format method to display it in the same format:

moment('14.05.2017 15:32','DD.MM.YYYY HH:mm').format('DD.MM.YYYY HH:mm')

Fiddle here

kiks73
  • 3,718
  • 3
  • 25
  • 52
  • yep it works now, but how to do it without directly entering the format because it can change as you change the language of the site – Wojna Dec 06 '17 at 19:33
  • You can try to set moment.locale('pl'); and at the next row momen(...) with the first argument only – kiks73 Dec 06 '17 at 20:03
  • @Wojna—you can't do that as you can't determine the format from the site (client?) language. – RobG Dec 06 '17 at 22:43