![Creative The name of the picture]()

Clash Royale CLAN TAG#URR8PPP
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
3 Answers
3
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();
The solution I'm suggesting will ignore the timezone itself. It always take only the date. It might be a complex 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');
I don't know if you are going to like my solution but just give it a thought that if you want to ignore timezone then why not from the string itself. Don't downvote if you don't like it. 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);
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Why not make a custom function to do it? Does it really have to be with moment.js?
– Er_sherlockian
yesterday