Displaying future time based on a browser's location

I have this date in my article:
Sunday, March 20 (11:55 PM EST)

It would be nice to put the time based on the location of the user:
Sunday, March 20 (11:55 PM local, 3:55 your time)

How is this done? Is it possible? (No JQuery, please. Client-side only.)

This should give you the general idea of how its done:

year = 2016;
month = 2;
day = 20;
hour = 23;
minute = 55;
second = 0;
dt = new Date(Date.UTC(year, month, day, hour, minute, second));
console.log(dt.toString());

or

dtutc = new Date('March 20, 2016 23:55:00');
dt = new Date(Date.UTC(dtutc.getFullYear(), dtutc.getMonth(), dtutc.getDate(), dtutc.getHours(), dtutc.getMinutes(), dtutc.getSeconds()));
console.log(dt.toString());

Note that the date/time you start with needs to be UTC as their computer will not know your computer’s timezone. Once you run either of the above then the dt variable will contain your visitor’s time equivalent to the UTC time you initially set.

When I run the above I get the following result:

“Mon Mar 21 2016 10:55:00 GMT+1100 (AUS Eastern Summer Time)”

What I get with your code is:
Sunday, March 20 11:55 PM EDT – your time: Sun Mar 20 2016 16:55:00 GMT-0700 (Pacific Standard Time).

Thanks!

Did you convert the date/time you wanted from your timezone to UTC before loading it?

The time is correct. For some reason, the event is on the west coast (where I am) but the time is calculated for Eastern. Don’t ask why! So the time difference between the two is correct after all.

However, this is not the military we are targeting, so having to count in my head to 16:55 is counter-intuitive.

What has the military got to do with it. Counting the hours of the day from 0 through 23 is very common in countries around the world while counting 1 to 12 twice if far less common. Its also easier for computers to simply count all the hours once so they go with the way most people expect.

I did not convert the date/time etc., before loading it. I’ll learn about how to do that next. The gap in hours, I just found out, is too large.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.