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"
Related
I have a list of dates (strings) from a delimited text file. All of these times are UTC. E.g 3 seconds past midnight on 1st July UTC
raw_time = 2022-07-01 00:00:03
I have converted this to a date using
my_time = new Date(raw_time)
I wish to test if the dates fall within a range
e.g.
Fri, 01 Jul 2022 00:00:00 GMT
to
Sun, 31 Jul 2022 23:59:59 GMT
The example fails because when I look at
my_time.toUTCString()
I get the result
Thu, 30 Jun 2022 23:00:03 GMT
I should add that I am in the UK on BST (GMT+1)
How can I force the raw date to be converted as a UTC date?
The problem is that your timestamps might be written in UTC timezone, but they are not valid UTC timestamps as per ISO standard: YYYY-MM-DDTHH:mm:ss.sssZ
var myDate = new Date("2022-07-01 00:00:03");
myDate.toUTCString()
//'Thu, 30 Jun 2022 23:00:03 GMT'
var utcDate = new Date("2022-07-01T00:00:03Z"); //note the Z character
utcDate.toUTCString();
//'Fri, 01 Jul 2022 00:00:03 GMT'
The best way to solve the issue would be to update your timestamps file with the correct format. If you for some reason can't, then you can modify the timestamp on the JS side by changing the string:
//raw_time is "2022-07-01 00:00:03"
const formattedTimestamp = raw_time.replace(' ','T').concat('Z')
// formattedTimestamp becomes "2022-07-01T00:00:03Z"
I guess the issue is because your local machine is in GMT Timezone, so when you do new Date(), it gives you Thu, 30 Jun 2022 23:00:03 GMT.
You can use moment timezone, where you can specify the timezone details like Canada, UK.
Please refer below:
https://momentjs.com/timezone/
Also you can refer to below link:
Momentjs: How to convert date/time of one timezone to UTC date/time
Hope this might help.
I have a collection of dates and times, it is formatted like this:
01.07.2013 16:10.
I know I have to rearrange to match ISO standard, so I managed to end up with a string like this:
2013-07-01T16:10.
From this, I need to create a Date Object. After reading many questions on here about this I am a little confused about how to add a timezone to this. All of the times are in New York Cities local time, which I also want to store the dates in.
So since Javascript takes the timezone from my machine, I changed it to be the one from NYC. However, the following part confuses me the most:
> print(new Date("2013-07-01T16:10"))
Mon Jul 01 2013 16:10:00 GMT+2000 (EDT)
Question: Why does it say GMT+2000 when my local time is GMT-0400 (the one of NYC)? Is this 'correct'? If not, how is this done properly?
--Updates:
I am using mongoDB 3.4.4 and interpreter Version is MozJS-38. The systemsetup -gettimezone returns America/New_York. Running mongo-express in chrome shows: Mon Jul 01 2013 06:00:00 GMT+0200 (EDT) for ISODate("2013-07-01T04:00:00.000Z") stored, but the shell says Tue Jul 02 2013 00:00:00 GMT+2000 (EDT) for print(new Date("2013-07-01T04:00:00.000Z"));.
You didn't specify the time zone offset when creating the Date object.
> new Date("2013-07-01T16:10-04:00")
2013-07-01T20:10:00.000Z
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/
I am using bootstrap calendar and on clicking any particular date I got complicated date in this form:
Sat Sep 06 2014 00:00:00 GMT 0500 (Pakistan Standard Time)
Can anybody please tell me how I can extract only date from this complicated long date?
I am working in PHP codeignitor, is there any way in PHP or JavaScript through which I can only extract date?
if this is for Javascript then just use this:
var myDate = new Date('Sat Sep 06 2014 00:00:00 GMT 0500 (Pakistan Standard Time)');
after that you can use myDate as a date object and call its methods like myDate.getFullYear() etc.
I have a set of dates w/ or wo/ year, like the following:
10/1/2010
10/1
1-Oct
1-Oct 2006
Jan 4
Jan 4, 2008
When I tried to parse them using new Date(),
for those date that don't have year, I got year as 2001
e.g.
a=new Date("Jan 4")
Thu Jan 04 2001 00:00:00 GMT-0500 (EST)
I want to parse all the dates. If they have year, use that year(2010, 2006, 2008 in the above dates). otherwise, use 2011
Anyone can help?
Thanks,
Cheng
Use DateJS for parsing dates. The Date constructor's parser is very limited.