Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moment js - get date without considering time zone

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);
like image 487
user2936008 Avatar asked Jul 25 '18 21:07

user2936008


People also ask

How can I date without a time zone?

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.

Does moment use UTC by default?

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.

How do I get moments from date and time zone?

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.

Does MomentJs use local timezone?

moment. tz. setDefault(String); By default, moment objects are created in the local time zone.


1 Answers

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();
like image 65
Phil Golding Avatar answered Sep 24 '22 16:09

Phil Golding