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 :)
To convert a date to another time zone: Use the toLocaleString() method to get a string that represents the date according to the provided time zone. Pass the result to the Date() constructor. The returned Date object will have its date and time set according to the provided time zone.
To convert Time to another TimeZone we can use the ConvertTimeZoneService class method by passing the DateTime value to that method which we want to convert.
The client's timezone offset could be detected by using the Date object's getTimezoneOffset() method. The getTimezoneOffset() method returns the time difference between UTC time and local time, that is the time offset, in minutes.
Use the toLocaleString() method to convert local time to EST, e.g. date. toLocaleString('en-US', {timeZone: 'America/New_York'}) . The toLocaleString method returns a string that represents the date and time according to the provided time zone. Copied!
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());
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With