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 :)

0

2 Answers 2

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());


}
Sign up to request clarification or add additional context in comments.

5 Comments

This works! How can I convert the time back to the users timezone, for displaying the datetime on the page?
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.
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(); }'
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.
Great work man!! Using your solution with little bit of trick setInterval({ convertToServerTimeZone() },1000) I was able to trigger realtime Multi-timezone clock
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.