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);
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);
Share
Improve this question
edited Sep 16, 2021 at 14:15
Nimantha
6,4716 gold badges31 silver badges76 bronze badges
asked Jul 25, 2018 at 21:02
user2936008user2936008
1,3476 gold badges20 silver badges43 bronze badges
4
- Why not make a custom function to do it? Does it really have to be with moment.js? – Sookie Singh Commented Jul 25, 2018 at 21:08
- custom function, I also want to format the date. – user2936008 Commented Jul 25, 2018 at 21:09
- I think: new Date also considers timezone. How can I make custom function, With all the cases – user2936008 Commented Jul 25, 2018 at 21:18
- Possible duplicate of JSON Stringify changes time of date because of UTC – Michael Freidgeim Commented May 25, 2019 at 0:59
3 Answers
Reset to default 13Have 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();
The solution I'm suggesting will ignore the timezone itself. It always take only the date. It might be a plex way to do it, but it always works for you.
const start = '2018-06-10T21:00:00-04:00'.split('-').slice(0, -1).join('-');
const end = '2018-06-10T23:00:00-04:00'.split('-').slice(0, -1).join('-');
const noconversion = moment(start).format('MM/DD/YYYY');
const converted = moment(end).format('MM/DD/YYYY');
If you want to ignore timezone then why not from the string itself? This is just an out of the box thinking.
function convertToDate(date_string) {
var date = date_string.indexOf('T') > -1 ? new Date(date_string.split('T')[0]) : date_string.indexOf(' ') > -1 ? new Date(date_string.split(' ')[0]) : new Date(date_string.substring(0,10));
return (date.getMonth() + 1) + "/" + date.getDate() + "/" + date.getFullYear();
}
OR
function convertToDate(date_string) {
var date = date_string.indexOf('T') > -1 ? date_string.split('T')[0].split('-') : date_string.indexOf(' ') > -1 ? date_string.split(' ')[0].split('-') : date_string.substring(0,10).split('-');
return date[1] + "/" + date[2] + "/" + date[0];
}
const start = '2018-06-10T21:00:00-04:00';
const converted = convertToDate(start);