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.

Related Threads:

How to set locale for parsing in moment.js?
How to change moment.js timezone defaults?
How to remove 00 in timezone moment.js?
How to convert back to another timezone in moment.js?
How to shorten the timezone name list from moment.js?
How to convert from utc to another timezone using moment?