Converting Timestamp to Date gives different timezones in the result? - javascript

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.

Related

How to convert PST timezone date to Local TimeZone in Javascript?

I'm getting the date in PST TimeZone and need to convert it to local TimeZone. I'm developing a product in CRM so TimeZone can be changed user to user.
I tried to use moment JS but for that, we need to specify the country/ city name and all I'm getting is TimeZoneOffset. I have a code for UTC to local time zone if it gets converted to UTC also then my work will be done.
Fri Jan 17 2020 22:57:49 GMT-0800 (Pacific Standard Time)
Need the answer in TS if possible or JS will be ok. Thank you.
To Convert other time zones to local time zone. You can use the following in javascript.
var date = new Date(other time zone value);
date.toString();
For example
var date = new Date('Fri Jan 17 2020 22:57:49 GMT-0800 (Pacific Standard Time)');
date.toString();
result will be "Sat Jan 18 2020 12:27:49 GMT+0530 (India Standard Time)"

How to convert time to specific string?

I'm working with a date that looks like this:
Mon Feb 04 2019 15:57:02 GMT-0700 (Mountain Standard Time)
and I'm trying to convert it to this:
2019-02-04T15:57:02.000Z
but for some reason my code always adds 7 hours and ends up being like this:
"2019-02-05T22:57:02.000Z"
Can anyone tell me what I'm doing wrong? Thanks a lot in advance!
Here's my code:
new Date(myTime as string).toISOString();
I'd use Moment.js, which is a decent date parsing and formatting library. To get what you're looking for, you'd use a statement like:
console.log(moment
.parseZone(
"Mon Feb 04 2019 15:57:02 GMT-0700 (Mountain Standard Time)",
"ddd MMM DD YYYY HH:mm:ss 'GMT'ZZ") // the format of the string presented
.local()
.format('YYYY-MM-DDTHH:mm:ss')); // the format of the output
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
I've broken the single line out into parts so it's a bit easier to read. A few notes:
parseZone allows you to parse the "-0700" from the string.
local converts the date from the parsed time zone to the current time zone
format formats the date.
The format topic has a list of the formatting tokens used.
The Parse > String + Format topic lists the parsing tokens (which are the same as the formatting tokens for the most part).
Note that the output does not have a "Z" at the end; this is important because without the "Z", it is a local date. With the "Z" you are are actually specifying a date and time that is 7 hours earlier than the one you've been given.
I'm not sure how to get this as a one-liner, but this is one way:
var time = new Date('Mon Feb 04 2019 15:57:02 GMT-0700 (Mountain Standard Time)')
new Date(time.setHours(time.getHours() + 7)).toISOString()
"2019-02-05T12:57:02.000Z"
Your code is not adding hours to the input date. What is happening is that your date string is using a particular timezone GMT-0700 (Mountain Standard Time) and the time zone used in new Date().toISOString() is UTC GMT+0000 (UTC). So when in the Mountain Standard Time timezone is Mon Feb 04 2019 15:57:02, in the UTC timezone is actually 2019-02-05T22:57:02.000Z. There are your seven hours from GMT-0700 to GMT+0000.
EDITED
If you don't really care about time zones and want to obtain 2019-02-04T15:57:02.000Z from Mon Feb 04 2019 15:57:02 GMT-0700 (Mountain Standard Time) you could just strip everything after GMT to let new Date() think it is an UTC date.
var timeString = 'Mon Feb 04 2019 15:57:02 GMT-0700 (Mountain Standard Time)';
new Date(timeString.substr(0, timeString.indexOf('GMT') + 3));
2019-02-04T15:57:02.000Z

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.

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.

JavaScript date values in different timezones

I am working with JavaScript date's being returned from ASP.net which is of course that really strange /Date(1328261701393)/ thing.
So I am parsing it out and doing...
var date = new Date(1328261701393);
console.log(date.toString()) // Fri Feb 03 2012 03:35:01 GMT-0600 (Central Standard Time)
When I change my system clock to EST, I get...
Fri Feb 03 2012 04:35:01 GMT-0500 (US Eastern Standard Time)
I THINK I understand why this is, but I am not entirely sure...
Also, I noticed that when I pass in an actual date string like...
console.log(new Date("2/1/2012 2:45:53 PM").toString());
I get the same time on both EST and CST...
Wed Feb 01 2012 14:45:53 GMT-0600 (Central Standard Time)
Wed Feb 01 2012 14:45:53 GMT-0500 (US Eastern Standard Time)
This all sort of makes sense, I was just looking for the WHY?
Javascript gives the time as per time zone of the browser. So, when you change your system clock, the time difference between your machine time and GMT changes. This difference is appearing in figures after GMT
You should use DateTime.ToUniversalTime() method to get UTC time, otherwise .NET will use server's current timezone.
http://msdn.microsoft.com/en-us/library/system.datetime.touniversaltime.aspx
Timestamp (like 1328261701393) is timezone independent,
Datetime(like 2/1/2012 2:45:53 PM) is timezone dependent.

Categories