I did read different StackOverflow posts and they suggested to use .utc from moment but it doesn't work
Note: I am on PST zone
const start = '2018-06-10T21:00:00-04:00';
const end = '2018-06-10T23:00:00-04:00';
const noconversion = moment.utc(start).format('MM/DD/YYYY');
const converted = moment(end).format('MM/DD/YYYY');
Current output:
noconversion - 2018-06-11
converted - 2018-06-11
Output expected: 06/10/2018 just fetch date from date provided
CODEPEN Link
const date = '2018-06-16T00:00:00-04:00';
const oldConversion = moment(date).format('MM/DD/YYYY');
const newConversion = moment.parseZone(date).format('MM/DD/YYYY');
alert('********oldConversion**********'+ oldConversion);
alert('********newConversion**********'+ newConversion);
To create a date without timezone:Get the ISO representation of the date string. Remove the Z character from the end of the ISO string. Pass the result to the Date() constructor.
utc(Date); By default, moment parses and displays in local time. If you want to parse or display a moment in UTC, you can use moment.
You can use moment.tz() to get timezone full name. It will return undefined if timezone is not set. Beside, you can use moment. format('zz') to get short timezone name.
moment. tz. setDefault(String); By default, moment objects are created in the local time zone.
Have you tried parseZone?
moment.parseZone(end).format('MM/DD/YYYY');
That should keep your UTC offset applied. You can then also calculate the UTC offset, if you wanted to save that:
moment.parseZone(end).format('MM/DD/YYYY').utcOffset();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With