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.
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.
This question already has answers here:
Incorrect date shown in new Date() in JavaScript
(3 answers)
Closed 5 years ago.
When I convert that date to ISO string I get the 30th of november 2015. Why wouldn't it be first of december? I have googled and I know that month is 0-indexed and that overflows lead to the next day/month/year. But I cannot explain myself that behaviour and when I google it I find unrelated topics.
Thing is if You type:
var date = new Date(2015, 11, 1);
console.log(date);
You will get output based on your timezone, for me it's:
Tue Dec 01 2015 00:00:00 GMT+0100 (Central Europe Standard Time)
Function toISOString will always output time in UTC. So in this case you will get this date minus one hour.
2015-11-30T23:00:00.000Z
If you check the MDN page you'll see that:
Note: Where Date is called as a constructor with more than one argument, the specifed arguments represent local time. If UTC is desired, use new Date(Date.UTC(...)) with the same arguments.
Your users have different local timezones.
For me new Date(2015, 11, 1) gives Tue Dec 01 2015 00:00:00 GMT+0100 (Romance Standard Time) (I'm in the timezone Central European Time which is GMT+1).
Hence you can follow the MDN hint and use Date.UTC inside of your date call instead:
var date = new Date(2015, 11, 1);
console.log(date.toString());
// "Tue Dec 01 2015 00:00:00 GMT+0100 (Romance Standard Time)"
// 00:00:00 in GMT+1 but 23:00:00 in GMT+0
console.log(date.toISOString());
// "2015-11-30T23:00:00.000Z"
// ^^ 30th of november - that's a nogo!
var utcDate = new Date(Date.UTC(2015, 11, 1));
console.log(utcDate.toString());
// "Tue Dec 01 2015 01:00:00 GMT+0100 (Romance Standard Time) 15:49:26.146"
// 01:00:00 in GMT+1 but 00:00:00 in GMT+0
console.log(utcDate.toISOString());
// "2015-12-01T00:00:00.000Z"
// ^^ The first! Not the 30th!
var date = new Date(Date.UTC(2015, 11, 1));
console.log(date.toISOString());
Output:
2015-12-01T02:00:00.000Z
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"
So I have this date returned from an API:
"2014-08-07T00:00Z"
And the results from new Date("2014-08-07T00:00Z") equal Wed Aug 06 2014 20:00:00 GMT-0400 (EDT) and .getDay() on that date gives me 3.
Why is it going from August 7th, to 6th, and the getDay returns 3?
Basically I'm trying to turn the API return date into english.
days[d.getDay()]+", "+months[d.getMonth()]+" "+getOrdinal(d.getDay())+" "+formatAMPM(d)
(aka "Wednesday, August 3rd 8:00 pm")
console.log(d,data[i].startDate, d.getDay());
//yields
// Wed Aug 06 2014 20:00:00 GMT-0400 (EDT) "2014-08-07T00:00Z" 3
days is just an array of text days, as is months, getOrdinal is a function that gives the text st or nd or rd on the day, and formatAMPM is pretty obvious.
.getDay() returns the day of the week. I think you'd expect .getDate()
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getDay
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getDate
That's why you're seeing that it is a Wednesday.
The result for me of new Date("2014-08-07T00:00Z") is Wed Aug 06 2014 18:00:00 GMT-0600 (MDT). This is because dates are converted to your local timezone when constructed
I don't think that there is any problem here.
Your date is 2014-08-07T00:00Z, with Z meaning Zulu Time Zone (equivalent to UTC), and using new Date() on it will convert to you local time, here GMT-4, that is why you are getting 4 hours difference.
And For the "3", the getDay() method returns the day of the week, wednesday in your case.
I have Date String Thu May 23 2013 18:19:32 GMT+0530 (India Standard Time) from my database. I want to make in this format THURSDAY May 23 2013 18:19:32 GMT 0500 CDT in ext-js.any idea ? Thanks in advance.
There are 2 excellent ate parsing libraries available tat you can use. They are both very small
https://code.google.com/p/datejs/
http://momentjs.com/
Sample datejs usage:
Date.parse('Thu, 1 July 2004 22:30:00 GMT') // Thu Jul 01 2004 16:30:00
You can then format the date object in whatever format you require for output.