Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert datetime from the users timezone to EST in javascript [duplicate]

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

like image 807
Carl Weis Avatar asked Jan 30 '12 20:01

Carl Weis


People also ask

How do I convert one time zone to another in JavaScript?

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.

How do I convert datetime to another 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.

How do I fetch the timezone of a user?

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.

How do you convert UTC to EST in typescript?

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!


1 Answers

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


}
like image 118
user47900 Avatar answered Oct 17 '22 08:10

user47900