15

I have a web application that has a dynamic javascript calendar, which allows users to set events for a given data and time. I need to push notifications to the users, so I need to convert the date time and timezone they entered, into Eastern Standard Time, so that my notifications are sent out at the correct time.

I would like to do this in javascript, so that when the data time value gets to the php, it's in the right format, before being added to the database.

So to summarize, I need to convert a javascript datatime and timezone, which I get by capturing the users datatime, as a full UTC date, to my servers timezone, which is EST - New York.

Any help or direction on this matter, would be greatly appreciated. Thanks :)

Carl Weis
  • 6,794
  • 15
  • 63
  • 86

2 Answers2

24

Pl check this script

function convertToServerTimeZone(){

    //EST
    offset = -5.0

    clientDate = new Date();
    utc = clientDate.getTime() + (clientDate.getTimezoneOffset() * 60000);

    serverDate = new Date(utc + (3600000*offset));

    alert (serverDate.toLocaleString());


}
user47900
  • 647
  • 4
  • 6
  • This works! How can I convert the time back to the users timezone, for displaying the datetime on the page? – Carl Weis Jan 30 '12 at 22:15
  • It's all in the offset. You can use user timezone offset and use functions available on date object to render in user locale specific format. – user47900 Jan 30 '12 at 22:24
  • It's much more simple if you intend to do using Javascript. As this runs on client browser , all that you need is code like below . 'function displayClientTimeZone(){ clientDate = new Date(); alert (clientDate.toLocaleString()); return clientDate.toLocaleString(); }' – user47900 Jan 31 '12 at 14:18
  • 6
    Be aware that while this solution converts to EST (North American EST, that is). It does not account for daylight saving time. So if you're looking for EST, this is fine. If you're looking for "US Eastern Time", composed of both Eastern Standard Time and Eastern Daylight Time, this approach will not work. [You will need a library to do that.](http://stackoverflow.com/a/15171030/634824) – Matt Johnson-Pint Jan 07 '14 at 16:05
  • Very useful. thanks – SamCodes Aug 10 '18 at 06:13
  • Great work man!! Using your solution with little bit of trick **setInterval({ convertToServerTimeZone() },1000)** I was able to trigger realtime Multi-timezone clock – Monkey D. Luffy Jan 06 '21 at 13:56
0

I would draw the EST timezone offset into the page while you're rendering it (via PHP) and then grab the UTC time on the client and offset it by the amount you've drawn in. Otherwise, if your server ever moves timezones your page will suddenly be reporting the wrong time.

I just did this recently though, and found that the simplest way to handle this is to actually use mySQL (I'm assuming that's your DB), and just let IT convert the timezone for you while you're entering the date stamp. Unless you're worried this is too tasking and you're trying to absolutely reduce the work your db is doing, I'd go that route, rather than muck about trying to do manual TZ conversions in JS.

Yevgeny Simkin
  • 27,946
  • 39
  • 137
  • 236