If javascript "(new Date()).getTime()" is run from 2 different Timezones - javascript

If JavaScript (new Date()).getTime() is run from 2 different timezones simultaneously, will you get the same value?
Will this value be affected by the system time set on the machine where the browser is running?

Yes, it's affected by system time. However, if the local time is correct (for whatever time zone the computer's set to), it should be the same in any time zone.
The ECMAScript standard says (ยง15.9.1.1):
"Time is measured in ECMAScript in
milliseconds since 01 January, 1970
UTC."

Code:
var today = new Date();
console.log(today);
var t = today.getTime();
console.log(t);
My Computer in the UK:
Sat Sep 21 2013 03:45:20 GMT+0100 (GMT Daylight Time)
1379731520112
My VPS:
Sat, 21 Sep 2013 02:44:31 GMT
1379731471743
Difference between getTime values is 48,369 milliseconds (48s) out of sync not the 1 hour zone difference

You won't get the same value - difference between two client's browsers picking up their system time, but if their time is set up ok, you should get two times with a minimal difference since getting the timestamp using new Date(), you can get the UTC value (new Date() returns number of milliseconds ellapsed since January 1, 1970, and that won't change), which is universal time and is location agnostic.

There will most likely always be a deviation between times attained between machines, but (I was wrong before) JavaScript Date() takes the UTC timezone as default.
Usually when time is essential, it's best to simply use the Server time and apply timezone corrections to that in the output if required.

Related

HTML5: hour from "Date()" object in <input type='time'> [duplicate]

I send this date from my controller in java (Spring-MVC) the type in mysql is datetime
#Temporal(TemporalType.TIMESTAMP)
#Column(name = "..") public Date getYy() {
return this.yy;
}
as : [2015-09-30 00:00:00.0]
When i get this dates with ajax as 1443567600000 :
new Date(1443567600000) convert to Tue Sep 29 2015 23:00:00 GMT+0000 (Maroc)
So why i get wrong date off by one hour?
SOLUTION
We resolve it by
d = new Date(value) ;
d.setTime( d.getTime() - new Date().getTimezoneOffset()*60*1000 );
because it was Daylight saving time (DST) or summer time problem. good article
This JS handling of Date is a quite a head-flip.
I'm in the UK... "easy" because we're on GMT (UTC)... except during the summer months, when there's DST (British Summer Time, BST). Clocks go forward in summer and back in winter (stupidly by the way, but that's another issue!) by one hour. One day in March what is 4pm GMT is now called 5pm (BST).
summer month:
If you do new Date( '2017-08-08' ) this will give you (toString) 'Date 2017-08-08T00:00:00.000Z'.
If you do new Date( '2017-08-08 00:00' ), however, this will give you 'Date 2017-08-07T23:00:00.000Z'!
In the second case it appears JS is trying to be "helpful" by assuming that because you stipulated the hour you were specifying BST time. So it adjusts to GMT/UTC. Otherwise it doesn't... though (of course) it still produces a Date object which is specific right down to the milliseconds. Quite a gotcha!
Confirmation: a winter month... when BST is not applied:
new Date( '2018-01-01 00:00' )/ new Date( '2018-01-01' ): both give 'Date 2018-01-01T00:00:00.000Z'
As for adjusting, it appears that to undo the automatic adjustment you just go
jsDate.setTime( jsDate.getTime() + jsDate.getTimezoneOffset() * 60 * 1000 );
... this is a bit like what Youssef has written... except you have to obtain the offset from the specific Date in question... and my experiments seem to prove that you add this, not subtract it.
Really it would be much more helpful if the default string representation of JS Date gave the UTC time followed by the TZ (time zone) offset information, as with some of the ISO date formats: clearly, this information is included.
I think maybe this is a Daylight Saving Time problem. You can check your client's timezone, and your server's timezone. (web server or SQL Server)
We should probably need more data about it, but it could be that nothing is wrong here, it depends how you set and get back your date.
Basically 1443567600000 doesn't contains timezone. It represent Tue Sep 29 2015 23:00:00 from Greenwich. It's a moment in time that, of course, it different from any location that has a different timezone. The same moment, happens at different time (the midnight of GMT+1 is the 11pm of GMT).
You have to store both the time and the timezone in your DB, and possibly send back to JS, otherwise it will be always interpreted differently depends by the local time of the client.
To make an example:
var d = new Date(2015, 8, 30);
console.log(d.toJSON()); // In my case I got "2015-09-29T22:00:00.000Z"
console.log(d.toDateString()); // "Wed Sep 30 2015"
To be more specific
time = new Date("2018-06-01 " + time);
var offset = time.getTimezoneOffset();
offset = Math.abs(offset / 60);
time.setHours(time.getHours() + offset);
in this case time is equal to hours with leading zeros:minutes with leading zeros.
This will add the hours difference to the UTC.
If you pass a string to date it is treated as UTC.

How to convert a UTC time to local time javascript

I have a requirement to convert a UTC time local time based on user timezone
I have two parameters utc time and users timezone as a string
ie
0,1,2,3 ...12 (timezone)
0,-1,-2,-3 ...-12 (timezone)
var utc = "2014-10-18T06:14:41.512Z"
tz = 5.5(Indian Standard Time)
Expected result Sat Oct 18 2014 11:44:28 GMT+0530
I have tried moment js
moment("2014-10-18T06:14:41.512Z").zone('+05:30').format('YYYY-MM-DD HH:mm')
and the result is correct.
But when i change the timezone to other it is not showing as expected result
tried
moment("2014-10-18T06:14:41.512Z").zone('+12:00').format('YYYY-MM-DD HH:mm')
result "2014-10-18 18:14" Expected 2014-10-18 19:18
12 is NewZeland timezone. Please help me to solve this issue. Thank you
Check this
var date = new Date('2014-10-19 17:00:34 UTC');
date.toString();
var timezone = "America/New_York";
var utcDate = "2014-10-19T10:31:59.0537721Z";
var localDate = moment.utc(utcDate).tz(timezone).format()
Also check
http://www.digitoffee.com/programming/get-local-time-utc-using-moment-js/94/
To adhere to international standards, you need to format your UTC date to include the time delimiter T, and the zone designator Z.
Z is the timezone designator for the zero UTC offset aka Zulu time.
You can read more about the International Date Standard ISO8601 format specifics here.
Once you've conformed to the international standard, the cross browser friendly approach is simple:
new Date('2014-10-19T17:00:34Z');
// Sun Oct 19 2014 12:00:34 GMT-0500 (Central Daylight Time)
A time zone is not an offset. An offset is only part of a time zone. Many time zones alternate between two different offsets to account for daylight saving time. The time zone has to account for this, including the specific dates and times that daylight saving time begins and ends, as well as any history of changes that the time zone may have had.
The New Zealand case you gave is a perfect example. You said "12 is New Zealand timezone", and thus expected since New Zealand is in DST for that date that the conversion from 6:14 UTC to New Zealand local time would be 19:14. - 13 hours later.
But 12 doesn't fully represent New Zealand. It is just a 12 hour offset from UTC. There are plenty of other time zones that use the same offset in different ways. For example, the Marshal Islands use UTC+12 year round, without daylight saving time.
You should really read the timezone tag wiki - especially the section titled "Time Zone != Offset".
Instead of offsets, you should represent time zones with their full IANA identifier from the tz database. For example US Eastern Time is "America/New_York", Indian Time is "Asia/Kolkata", and New Zealand Time is "Pacific/Auckland". You can find more in the list on Wikipedia.
You can use moment-timezone to work with these in JavaScript.
moment("2014-10-18T06:14:41.512Z").tz('Pacific/Auckland').format('YYYY-MM-DD HH:mm')
// Output: "2014-10-18 19:14"
I also cover these topics in great detail in my Date and Time Fundamentals course on Pluralsight.com.
Please Check this link
http://www.digitoffee.com/programming/get-local-time-utc-using-moment-js/94/
var timezone = "UTC+5.30";
var utcDate = "2014-10-19T10:31:59.0537721Z";
var localDate = moment.utc(utcDate).tz(timezone).format()

getUTCDate() Function Issue in Different Time Zones

I am facing very strange issue kindly take a look at two examples below.
My Development Environment Time Zone: GMT+0500
When i use following function:
var d = new Date("Tue Mar 18 2014 00:00:00 GMT+0500");
var n = d.getUTCDate();
n = 17 which is correct (Return the UTC day of the month of a specific, local time, date-time) and everything works perfectly in my timezone.
My Clients Time Zone: GMT+0000
var d = new Date("Tue Mar 18 2014 00:00:00 GMT+0000");
var n = d.getUTCDate();
n = 18 which is wrong
any one put some light why is that? how to resolve this issue?
any help would be appreciated.
n = 18 which is wrong
No it's not. You supplied GMT+0000, which is the same as GMT or UTC. So the result from getUTCDate is of course the date you passed in.
I think you are confused because of how you worded this:
My Development Environment Time Zone: GMT+0500
My Clients Time Zone: GMT+0000
A time zone is not a numeric offset. A time zone can have an offset, or multiple offset, and includes the history of how the offsets have changed over time. See "Time Zone != Offset" in the timezone tag wiki.
So those might be the current offsets for you and your client, but that doesn't necessarily mean that they are always going to be in the same offset. If your client is in the UK, then they are at +0000 now, but they will soon be on +0100. See here for details.
Your first date is explicitly constructed with a time zone that results in the UTC date being 17. At midnight of the 18th in the timezone 5 hours ahead of UTC (GMT), it's still the 17th in London. Your second date is constructed with the explicit UTC timezone. At the time indicated by your second date, in other words, it's 5 o'clock in the morning in the first timezone.

Difference between these dates in Javascript? And why do not all browsers give same result?

var dt = new Date("2012-04-23T12:00:00");
var dtz = new Date("2012-04-23T12:00:00Z");
If the Z is present I get a different time.
When the Z is present is it converting the Date to the browser's local time and when not present assuming it is already in local time?
I get different results in FF than Chrome. Chrome always gives me the same time. FF treats them as different. How should I be dealing with UTC dates from the server?
"Z" is a military time zone corresponding to UT (aka UTC, aka GMT). So basically, 'nnn Z' means "how late is it in your time zone when it's 'nnn' in Greenwich". For example, I'm in CEST which is GMT+2 so this
new Date("2012-04-23T12:00:00Z")
returns for me:
Mon Apr 23 2012 14:00:00 GMT+0200 (CEST)
As to dates with a TZ specifier, they seem to be treated differently in Firefox (which assumes local TZ) and Chrome (which assumes UTC). For safety, I'd suggest always using an explicit TZ specifier.
var dt = new Date("2012-04-23T12:00:00");
var dtz = new Date("2012-04-23T12:00:00Z");
tried it with alert() and got these messages
alert(dt);
Mon Apr 23 2012 12:00:00 GMT+0500 (West Asia Standard Time)
alert(dtz);
Mon Apr 23 2012 17:00:00 GMT+0500 (West Asia Standard Time)
it means that if you create the date without "Z", it returns browsers's local time at GMT, mentioning your time zone is below or above GMT
and if you create it with "Z", it will show the local time at your time zone, referring to your time zone.
According to ISO 8601, IF no UTC relation information is given with a time representation, the time is assumed to be in local time.
If can verify the correct behavior on both Firefox, Safari and Internet Explorer:
The following should return: false
new Date("2014-05-09T22:12:18.893Z").valueOf() === new Date("2014-05-09T22:12:18.893").valueOf()
If you try the same thing on Chrome or Opera, it will incorrect indicate: true
The moral of the story is, if you have a string in the above format, add a Z at the end.

Setting Javascript date object to midnight without being based on user computer date

I need to set a date and time to 12 midnight regardless of the user's computer date. I am creating an international meeting planner to offset time zones to get the meeting.
I have it working but I need to code now the differences in time zones. If I use new Date, it gives me the time based on the user's computer. For example, mine is Eastern US. If I try to do the time zone shift on November 6, 2011, Javascript/computer will calculate MY time zone shift at 2am. I do not want this.
My real goal is to set it to 12 midnight in the timezone that is where the meeting will be hosted from (let's say Afghanistan) and then calculate from there.
So:
How do I set 12 midnight without being being the user's computer time?
Can I set 12 midnight to a specific time zone, without being dependent on user's computer time?
I have to do this with Javascript as there is no server code involved.
Thanks
Something like this I would guess: new Date(Date.UTC(year, month, day, hour, minute, second))
w3schools Reference
To create midnight in GMT:
// Note: months are 0-based, so 7 == august
var midnight = new Date( Date.UTC(2011,7,15) );
// Sun Aug 14 2011 18:00:00 GMT-0600 (Mountain Daylight Time)
To create midnight in another time zone:
var mdt = -6; // Mountain Daylight Time
var midnightMDT = new Date( Date.UTC(2011,7,15,-mdt) );
//-> Mon Aug 15 2011 00:00:00 GMT-0600 (Mountain Daylight Time)
Date objects are expressed in the local time zone of the user, but they are still representative of midnight in another time zone.
If you want to express a date in another time zone, you'll need to offset the date to that timezone (setUTCHours()) and then use the various getUTC* methods (e.g. getUTCHours()) to construct your own string.

Categories