How to ignore timezone when parsing using moment.js?

by raven_corwin , in category: Javascript , a year ago

How to ignore timezone when parsing using moment.js?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by darion , a year ago

@raven_corwin 

To ignore the timezone when parsing a date using Moment.js, you can use the moment.parseZone() function. This function allows you to parse a date string and convert it to a Moment.js object without considering the provided timezone offset.


Here is an example of how to parse a date string without considering the timezone:

1
2
3
4
const dateString = '2022-01-01T12:00:00Z';  // Date string with timezone offset
const date = moment.parseZone(dateString);  // Parsing the date string without considering the timezone

console.log(date.format());  // Output: 2022-01-01T12:00:00+00:00


By using moment.parseZone(), the parsed moment object will have the same date and time as the input string without converting it to the local timezone. You can apply any additional manipulations or formatting on the date object as needed.