Skip to content Skip to sidebar Skip to footer

Processing Localised Date To Convert It To Local Date/time Using Moment.js

I am getting translated UTC date from the backend and need to convert the date to localised local time format before displaying on the screen. I have been using, moment.locale(cur

Solution 1:

You should give the parser the format of the string you're parsing. You must be ignoring the error you're getting.

var inputDate = 'inputDate = 2017年11月01日  AM10:42 (GMT)'var parseFormat = 'YYYY MM DD  Ah:mm';
var printFormat = 'MMMM Do YYYY, h:mm A';

var d = moment.utc(inputDate, parseFormat).toDate();
console.log('Local: ' + moment(d).format(printFormat));
console.log('UTC  : ' + moment.utc(d).format(printFormat));
<scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.19.1/moment.min.js"></script>

The technique of parsing a string to a moment object, then creating an ECMAScript Date just to parse it back into a moment.utc object is weird to me. There doesn't seem to be a way of parsing a string as UTC without then setting the output offset to UTC too.

Post a Comment for "Processing Localised Date To Convert It To Local Date/time Using Moment.js"