Wednesday, June 5, 2024
 Popular · Latest · Hot · Upcoming
18
rated 0 times [  19] [ 1]  / answers: 1 / hits: 19009  / 10 Years ago, tue, april 1, 2014, 12:00:00

The user selects a date from a jquery datepicker, it has following format:



DD.MM.YYYY


When the user submits the form, I want to transform the date input to UTC using moment. However whatever I do with the input, I get a Invalid Date error.



E.g. input is



30.03.2014


I try to format:



console.log(moment(input).format('MM/DD/YY')); <-- prints Invalid Date


I try to



console.log(moment(input).utc().toDate()); <-- prints invalid date


It seems that moment cannot parse the input, it can parse MM.DD.YY format, however the input must have DD.MM.YYYY format.



Any ideas whats wrong?






Using



moment(input, 'DD.MM.YYYY')


gives me:



{ from: '01.03.2014', to: '30.03.2014' }
{ from: Sat Mar 01 2014 00:00:00 GMT+0100 (CET),
to: Tue Apr 01 2014 11:00:00 GMT+0200 (CEST) }

More From » node.js

 Answers
11

Consider using moment's hinting support:



moment(input, 'DD.MM.YYYY')


A Fiddle demonstrating the approach is available here:



http://jsfiddle.net/x4Xm3/



Moment will assume local time inputs, so if you are working outside of that timezone you may want to append the timezone to your input. See the docs at http://momentjs.com/docs - in particular parseZone() and String + Format sections.



In the case of server-side (node), you are probably getting odd results because of the server's timezone.


[#71682] Saturday, March 29, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
melinal

Total Points: 367
Total Questions: 101
Total Answers: 96

Location: Nauru
Member since Thu, Feb 2, 2023
1 Year ago
melinal questions
Thu, Dec 2, 21, 00:00, 3 Years ago
Tue, Jun 15, 21, 00:00, 3 Years ago
Tue, Feb 25, 20, 00:00, 4 Years ago
;