JavaScript Date getHours returning weird result - javascript

I have a UTC timestamp stored on a cloud storage as "Sun Feb 22 02:30:00 UTC 2015".
In my javascript, I parsed the UTC time as string to create a Date object.
var obsDTStr = 'Sun Feb 22 10:30:00 CST 2015'; // this is correct as I am in Time Zone +8
console.log(obsDTStr); // console prints out "Sun Feb 22 10:30:00 CST 2015" which is also correct
var obsDT = new Date(obsDTStr); // create a Date object with the string
console.log(obsDT.toString()); // console prints out "Mon Feb 23 2015 00:30:00 GMT+0800 (China Standard Time)" which is incorrect
I am unsure why the result was so weirdly incorrect.
Any idea? Thanks!

TRY
console.log(obsDT.toUTCString());

Related

moment timezone js returns wrong datetime

I am using Moment Timezone to get current datetime for Asia/Tokyo timezone.
The code is as following
var currentDateTime = new Date(moment().tz('Asia/Tokyo').format('YYYY-MM-DDTHH:mm:ss'))
Supposed that the current datetime for my local timezone is as following
Thu Jul 22 2021 19:49:47 GMT+0700 (Indochina Time)
I expected the current date for Asia/Tokyo timezone would be as following
Thu Jul 22 2021 21:49:47 GMT+0700 (Indochina Time)
In Chrome I got the expected datetime.
But in Safari on my iPhone, I got the wrong datetime.
Fri Jul 23 2021 04:49:47 GMT+0700 (Indochina Time)
It seems the current datetime returned is the right current datetime plus 7 hours.
Here is my environment
iPhone : iPhone 6
iOS : 12.5.4
The problem you have is caused by the Date constructor parsing the time according to the local timezone (in Chrome) and UTC (on Safari?).
For the time given, this code
moment().tz('Asia/Tokyo').format('YYYY-MM-DDTHH:mm:ss')
returns
"2021-07-22T21:49:47"
You then pass this time into the Date constructor:
var currentDateTime = new Date("2021-07-22T21:49:47")
Because this string has no time zone information, it is assumed to be in the device's local timezone (Chrome) or UTC (Safari) which gives the wrong date object.
While you could inject the timezone into this string (using ZZ), you can build a Date object from the moment object using:
var currentDateTime = moment().tz('Asia/Tokyo').toDate()
but this is effectively the same as
var currentDateTime = new Date()
If the intended output is a string of the format:
"Thu Jul 22 2021 21:49:47 GMT+0900 (Japan Standard Time)"
the closest you could get is:
moment().tz('Asia/Tokyo').format('ddd MMM D YYYY, h:mm:ss a [GMT]ZZ (z)')
// Thu Jul 22 2021 21:49:47 GMT+0900 (JST)
Alternatively, there is also the localized "Month name, day of month, day of week, year, time" format:
moment().tz('Asia/Tokyo').format('llll (z)')
// In my locale, I get:
// Thu, Jul 22, 2021 9:49 PM (JST)
Take a look at the Moment display format documentation for more options.

Converting Timestamp to Date gives different timezones in the result?

I have a database where I have put down timestamps that are converted to UTC.
Now, when I am on PST, I convert 2 timestamps in Chrome, which get back to the following dates:
new Date(1446274800000)
Sat Oct 31 2015 00:00:00 GMT-0700 (PDT)
new Date(1448265600000)
Mon Nov 23 2015 00:00:00 GMT-0800 (PST)
They differ in timezone! How is this possible? I always just want to get back the PST time (or, preferably, the UTC time they were stored in).
Looks like it's daylight savings time.

Why is new Date() subtracting 1 day in Javascript?

I need to convert a String to a Date object.
The date string is delivered in the following format:
"2015-01-28T00:00:00"
When I create a new Date, I get the previous date:
Entered: new Date("2015-01-28T00:00:00")
Result: Tue Jan 27 2015 17:00:00 GMT-0700 (Mountain Standard Time)
Does anyone know why this is occurring?
When you enter the following:
new Date("2015-01-28T00:00:00");
// Result: Tue Jan 27 2015 17:00:00 GMT-0700 (Mountain Standard Time)
the browser assumes that you are giving a date in GMT Time zone. So it will automatically convert the given date to your local date.
It's always a good idea to inform the browser of the timezone you are working on in order to prevent future problems:
new Date("2015-01-28T00:00:00-07:00");
// Result: Tue Jan 28 2015 00:00:00 GMT-0700 (Mountain Standard Time)
Actually, you aren't getting the previous date . . . you are getting that date, offset by the timezone difference.
Tue Jan 27 2015 17:00:00(Mountain Time) + 7 hours (time zone difference) = 2015-01-28T00:00:00 (GMT)
Or, in English, when it is 12:00 Midnight in Greenwich, England, it is 5:00 PM on the previous day in Denver, Colorado. ;)
It's the right date/time, just in a different timezone.

Incorrect Javascript Date in Chrome vs Firefox

I'm getting incorrect dates in Chrome...
My code looks like this..
Title contains "2013-06-14T00:00:00", it was a DateTime in C# returned from WebAPI
As you can see here on both browsers..
When I add it to a new javascript date like this..
var dt = new Date(title)
I get different dates in different browsers...
Example - http://jsfiddle.net/RvUSq/
Looks like Firefox is assuming this datetime format without timezone is local time and Chrome/Webkit is assuming it's UTC.
If the datetime returned from the api is UTC, simply append a "Z" to the end of the string, so it becomes "2013-06-14T00:00:00Z", which indicates the time is in UTC, then you will get the same result in the two browsers.
Convert timestamp to ISO 8601 formatted string in C#, for e.g
var title = "14 JUN 2013 00:00:00" // printed from C#
Then use Date constructor
var date = new Date(title);
If you don't specify timezone the local timezone in the client machine will be set to the given time. If you specify the timezone, needed calculations will be done to convert the date to local timezone.
var title = "14 JUN 2013 00:00:00";
var date = new Date(title); // Fri Jun 14 2013 00:00:00 GMT+0530 (IST)
var title = "14 JUN 2013 00:00:00 GMT";
var date = new Date(title); // Fri Jun 14 2013 05:30:00 GMT+0530 (IST)
var title = "14 JUN 2013 00:00:00 GMT-0400";
var date = new Date(title); // Fri Jun 14 2013 09:30:00 GMT+0530 (IST)
ref: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse

Creating date with 31 as day gives 30 in output

I'm creating dates from strings with the format 'yyyy-MM-dd' but they're always created on the previous day for some reason. If I set the date as '2012-10-31' the Date object with actually be 30 of October and not 31. For example, this:
var d1=new Date('2012-10-31');
Will output this:
Tue Oct 30 2012 19:30:00 GMT-0430 (Venezuela Standard Time)
Can someone explain why this happens?
With no further parameters, Date() create your time-stamp with GMT+0000.
Converting your date to string with no further parameters either, it will use the localised notation.
If you want to create a date matching your time-zone, do:
var d1=new Date('2012-10-31 GMT-0430');
//That's what you should get
//"Wed Oct 31 2012 00:00:00 GMT-0430"
Using this date now, you can convert the local time to the time of other timezones if you execute d1.toString() in a browser with a different timezone:
d1.toString();
//That's what I get
//"Wed Oct 31 2012 05:30:00 GMT+0100"
Try this
var d1=new Date(2012, 10-1, 31, 0, 0 ,0);
document.write(d1);
which produces
Wed Oct 31 2012 00:00:00 GMT-0400 (Eastern Daylight Time)
the key is to remove the quotes and manually set the time. Also note that 'month' is zero based and so for readability I subtract one from it
This happens because dates are converted to a string based on your local time zone.
The date variable actually contains October 31st 0:00 UTC. When you convert it to string, it is converted using your own time-zone, which is 4:30 hours behind UTC.

Categories