javascript timezone converter - javascript

I need a way to convert times in the future to different timezones without relying on the user's computer time.
At registration time, the user supplies his timezone. When he logs in, I calculate the offset in minutes between the UTC time and his time and inject that offset into the page so that a javascript function can do the conversions. Something like this:
var TheUTCTime = new Date(UserTime.getTime() - TimeZoneOffsetInMinutes * 60000);
and like this for the other way around:
var TheUserTime = new Date(UTCTime.getTime() + TimeZoneOffsetInMinutes * 60000);
This works really well to convert times as long as the offset doesn't change. For instance, because of daylight saving, between US EST and UTC, there's a difference of 300 minutes or 360 minutes depending on the month in the year.
My functions work well to convert today's date but I'd like something that can 1) do the same thing for any day of the year and 2) doesn't depend on the user's internal clock or timezone.
How could I do this?
Thanks.

My functions work well to convert today's date but I'd like something that can 1) do the same thing for any day of the year and 2) doesn't depend on the user's internal clock or timezone.
If you want to convert another UTC time into the user's local time, you have to know their time zone. That's pretty much the definition of a time zone: a mapping between UTC and local time (or equivalently, between UTC and the offset from local time).
As you've seen, getting the current offset isn't enough, because of daylight saving transitions (and any other changes to time zones - they vary more than you might expect).
Basically there's no way round this: you will have to ask the user for their time zone. You can make a good guess based on the current offset from UTC and possibly geocoding their IP address, but you'll have to confirm it with them. (For example, they may be on a trip or something, and not in their "home" time zone.)

To don't depend on the user's clock timezone, I think the best approach is to do conversions in the server side.
There's a good question here in the SO that covers daylight saving time and javascript Date object: Daylight saving time and time zone best practices

Related

Date Format related

I have 2 Dates, One in UTC format and the other one in local time. Now I need to convert the local time to UTC by referring the UTC date time that is there. How to achieve this? Also, I want to make sure the local to UTC conversion handles daylight as well.
Eg: I have an UTC time 2023-02-16T10:00:00Z. and I have a regular time 2023-02-16T16:00:00. Assume the local time is PST, the offset should be -08:00. I understand i can do it in bruteforce way to achieve this. But is there an easy way to do this in JavaScript?

How to manually modify Unix TimeStamp so that its 5 hours behind UTC?

How can I modify a raw Unix timestamp so that it shows that it is 5 hours behind (as an example). I'm looking to do this with a javascript or python. The more lightweight the better. I'm basically looking for how to manually decode a given unix timestamp and change some of its numbers so that it gives me back a unix timestamp showing a different time. It would be even greater if I could automatically adjust it to a users personal time-zone using javascript/python.
Convert to the number of hours you want to offset by to seconds, and then add or subtract it from the Unix timestamp. As far as getting the user's personal time zone, I'm not sure how you would do that without language specific code.
How to choose a time 5 hours numerically smaller
This would be relevant for if, for example, you were testing whether the submission of an exam answer is within 5 hours of the exam's start time.
For this, just subtract 5 * 3600 * 1000 from the Unix timestamp's numerical value.
What you are actually proposing to do is extremely unwise
You seem to be planning to create a Unix timestamp of a different point in time which, when expressed as UTC but with the annotation "UTC" deleted, will match the local time display expected by a user who is 5 hours behind UTC. I can see why you are tempted to do this but it is a very bad idea.
Unix Timestamps do not default to be in UTC, they describe a point in time across all of space simultaneously. If you shift the value of a Unix timestamp, it is no longer a Unix timestamp, just as (mass of car minus 50 kg) is no longer the mass of that car. The value is either the mass of a different car that is 50kg lighter, or an incorrect value for the mass of the original car.
Unix timestamps are unambiguous. Once you know that a variable contains a Unix timestamp, you can stop worrying about any if's, but's or maybe's. It is solid and definite. What you are creating is a horrible thing which looks like a Unix timestamp of an timepoint, but it is not. What variable name are you going to give it to prevent confusion? You might give the physical property a new name, such as the goalieTimeStamp, which is distinguished from Unix timestamps by being displaced by 5 hours.
If a person is 5 hours behind UTC now (in January), that person will likely be a different number of hours behind UTC in summertime. This is a mess.
I think you are doing this so that you can display a local time nicely. Choose a different, better, way to achieve this.
You should use the localisation system in the relevant language to obtain and display the local time, which will depend not only on the location of the user, but also the time of year. This will also allow you to deal with languages etc, if you need to.
And throughout your code you will have a clear distinction between the timepoint of your event (invariant across space) and how a local user will express that time in their timezone, time of year and language.
A good library for this in Javascript is moment.js. It is rather heavyweight, but this is because the task is much more heavyweight that it first seems!

Convert set date to user's local time automatically

I am creating an application where I have a pretty big set of dates and times, and I need to display these in the user's local time and date. All set dates and times are in BST; so for example 08-24-2014 16:00 BST = 08-24-2014 11:00 EST. Now, before coming here I spent good 3-4 hours looking for an answer but if anything, I got more confused. Is there any way, to convert a set of BST dates and times to the user's local settings automatically without them setting the time zone etc?
p.s.: I have two ideas in mind but I don't know if they would work nor how to execute them.
1) Get and change the BST date and time and convert it to a unit of measurement; get and change the user's local date and time and covert it to the same unit of measurement as above, calculate the difference in the new measurement, and convert that to the user's local time.
2) Use GeoLocation to find the user's date and time/ time zone and; convert the BST to whatever the GeoLocation spits out.
you can get the users machine timezone in javascript:
var currentDate = new Date();
var currentTimeZoneOffsetInHours = currentDate.getTimezoneOffset() / 60;
see documentation here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getTimezoneOffset
having the offset you can add it to your values
there is a javascript library Moment.js which allows you to query daylight saving values for specific dates and timezones.
because all your dates are in british summer time. first query if a date is in british winter time with isDSTShifted(). and subtract() an hour.
convert the date to another timezone

Is there a simple time to get a timezone from a given time?

I need to get a timezone from a time, date is not important, but daylight savings is.
something like:
timezone = function("15:00");
Is there a simple way to do this?
I dont think you can get the timezone from the time but you might get some help from Date.prototype.getTimezoneOffset()
The getTimezoneOffset() method returns the time-zone offset from UTC,
in minutes, for the current locale.
Example:
var x = new Date();
var currentTimeZoneOffsetInHours = x.getTimezoneOffset() / 60;
No, of course not. Think about it, you're passing 15:00 to that function, presumable denoting it's 3PM. But in what timezone is it 3 PM? No way of knowing. It's like me saying it's quarter to, without saying what hour it's quarter to to.
The only way you can get a timezone in JS is by using the Date object, but that just shows the timezone of the machine on which your code is running, nothing about the TZ that "created" the time you're processing.
Also note that daylight saving isn't a global phenomenon, quite the contrary. AFAIKT, there isn't a single time-zone where DST has always been in place...
In order to get TimeZone information you need more than a Date (and an offset). You need a location.
Javascript does not know the location that it resides in but it does know the current offset from UTC. That is different than a Time Zone. The daylight savings time issue play havoc with this key difference.
This has posed problems when dealing with server applications that know their timezone and report dates as being in a specific Time Zone.
My rule of thumb has been fairly simple in this regard.
Always use Long or long (a 64 bit number) to store, pass and process dates times or intervals, only convert to Date, Calendar or DateTime objects when interacting with people.
Once you have a date object, such as with new Date(), you can use .getTimezoneOffset() to get the number of minutes between the date's object and UTC, which is timezone information you can use.

Deal with timezones in JS from UNIX timestamp

I'm currently writing a small messaging app for global usage. I'm going to store the UTC timestamp of the message. I need to display the message to the user using AJAX, so i need to convert the UNIX timestamp to the users local time. I know how to get the timezone offset, but i'm afraid that it will be a little bit inaccurate and i need accuracy even in seconds (to display times like: 34 seconds ago). Which is the most accurate way to solve this?
Why not calculate the local date locally? Constructing a date with new Date(millisecondsSinceEpoch) locally and then calling toTimeString() (or whatever) should show you the local time:
new Date(milliseconds).toTimeString()
var d = new Date((new Date(1390312399000)).toUTCString());
Where the timestamp is the stored UTC timestamp.

Categories

Resources