Moment js - get date without considering time zone

The name of the pictureThe name of the pictureThe name of the pictureClash 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





Why not make a custom function to do it? Does it really have to be with moment.js?
– Er_sherlockian
yesterday





custom function, I also want to format the date.
– user2936008
yesterday





I think: new Date also considers timezone. How can I make custom function, With all the cases
– user2936008
yesterday






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();





I did try it I did had the same issue in one of the time zone. I will check again
– user2936008
yesterday





This one worked for me.
– Kamalakannan J
yesterday





@KamalakannanJ from momentjs.com/docs/#/parsing/parse-zone seems like there is some conversion going on
– user2936008
yesterday





I did take this approach as it resolves EST/PST issue.
– user2936008
yesterday



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');





What if the timezone is not provided? This will fail in that case. I wouldn't take that risk. I would never muck with the string provided.
– user2936008
yesterday







Valid point, I agree that.
– Kamalakannan J
yesterday



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);





What if the timezone is not provided? This will fail in that case. I wouldn't take that risk. I would never muck with the string provided.
– user2936008
yesterday







It won't fail even there is no timezone as I am splitting with T separator which is in all date standards.
– Er_sherlockian
yesterday





Still I am afraid to use string split approach
– user2936008
yesterday





I have added one more function which uses Date function.
– Er_sherlockian
yesterday






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.

Popular posts from this blog

How to scale/resize CVPixelBufferRef in objective C, iOS

Stripe::AuthenticationError No API key provided. Set your API key using “Stripe.api_key = ”

SVG with two text elements. When one resizes due to textLength - how to resize the other one to the same character size