Issue with Date formats conversion - javascript

I have the following date format : 2020-09-25T11:09:00.422Z, which in order to be processed, needs to be modified to the following format: Wed Apr 01 2015 00:00:00 GMT+0100
How to achieve that in a one liner ?
Thanks

Create a Date instance and pass the value as string:
new Date("2020-09-25T11:09:00.422Z")

Related

How to convert new Date() to UTC date using moment JS

I want to convert the returned value of new Date to UTC format using moment js.
I tried using moment.utc but
moment.utc(moment(new Date())).format();
output -> 2020-01-01T04:00:00Z
but I want output in this format
Thu Jun 04 2020 00:00:15 GMT+0530 (India Standard Time)
just like what new Date returns but it should be in UTC.
and the other problem is that the return type of format is of
TYPE : STRING I want that the format should be the same but its type should be of date object.
Any help would be appreciated.

When I try to create a date object from another date format, the result date is changing it's value

When I try to create a date object from another date format, the result date is changing it's value. How to achieve this without changing the date value ?
new Date("Mon, 31 Oct 2016 00:00:00 GMT");
the result coming as Sun Oct 30 2016 20:00:00 GMT-0400 (Eastern Daylight Time), How can I get the Monday 31 date from the above?
Adjusting the timezoneOffset from the created date object should do the trick,
but be cautious while using it , as you should be sure that the date object was created from GMT not from some local time .
And the below answer has been posted assuming the input date was in GMT
var tempDate = new Date("Mon, 31 Oct 2016 00:00:00 GMT");
var tempTime = tempDate.getTime() + (tempDate.getTimezoneOffset() * 60000);
tempDate = new Date(tempTime);
console.log(tempDate);
It doesn't change the date, it just converts it to your local timezone. This is a bit of annoying behaviour and the only way I know to get around it is to set your system timezone to GMT. If you need to do date and time work, you might want to look at Moment.js - http://momentjs.com/

Javascript Date object different depending on format: yyyy-mm-dd vs mm-dd-yyyy

Another JavaScript Date conundrum...
I get two completely different Date objects depending on the format I pass to the Date constructor (Date.parse() also produces different results).
Example:
new Date('04-27-2016'); => Wed Apr 27 2016 00:00:00 GMT-0500 (Central Daylight Time)
Date.parse('04-27-2016'); => 1461733200000
vs
new Date('2016-04-27'); Apr 26 2016 19:00:00 GMT-0500 (Central Daylight Time)
Date.parse('04-27-2016'); => 1461715200000
I can easily work around this situation in my code, but I would like an explanation as to why/how this is doing what it does.
The ISO format you use in the second example has UTC as its default timezone. It's described here on MDN. If you want the correct behaviour for this format, you have to add the information about timezone: new Date('2016-04-27 GMT-0500'); or you have to also specify the time: new Date('2016-04-27 00:00:00');
In my opinion it's better not to use the built-in parser and use some library instead. For example Moment.js is great.

convert month string to date

I'm using some library that won't sort objects by a string value but will sort them by date. I have months like '2008-04' and I should be able to convert them to Javascript dates for the first of the appropriate month. But my local timezone screws things up:
new Date('2008-04')
Mon Mar 31 2008 20:00:00 GMT-0400 (EDT)
This is probably a duplicate of How do you convert a JavaScript date to UTC?, but maybe there's a simpler answer for my particular use case than the ones there?
BTW, I get the same answer by specifying the first of the month:
new Date('2008-04-01')
Mon Mar 31 2008 20:00:00 GMT-0400 (EDT)
I'm using ES6. I don't suppose that makes it any more straightforward?
Add '-01T00:00:00Z' to the string with part of ISO 6801 date:
document.write(new Date('2008-04' + '-01T00:00:00Z'));
I misunderstood. The date is already UTC, it's just when I display it as a string locally that it gets converted to my local timezone. So the answer is just
new Date('2008-04').toUTCString()
"Tue, 01 Apr 2008 00:00:00 GMT"
or
new Date('2008-04').toISOString()
"2008-04-01T00:00:00.000Z"

Javascript date object automatically add one day when creating from date string

In my javascript i want to convert date from date string.
i have string like
date = "Thu Sep 03 2015 19:30:00 GMT+0000"
Now i convert string using Date object.
var d = new Date(date);
But this gives me,
Fri Sep 04 2015 01:00:00 GMT+0530 (IST)
It automatically add one day into day. What is wrong?
It automatically add one day into day. What is wrong?
Nothing. The time you input is 19:30 GMT and the timezone on the device you're using is set to GMT+0530. Add 5 hours 30 minutes to 7:30pm and you get 01:00am the following day.
You should not use the Date constructor to parse strings, as it is inconsistent across browsers and until recently, entirely implementation dependent. Manually parse strings, or use a Date library.

Categories

Resources