如何将javascript中的datetime从用户时区转换为EST [英] How to convert datetime from the users timezone to EST in javascript

查看:425
本文介绍了如何将javascript中的datetime从用户时区转换为EST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有动态JavaScript日历的Web应用程序,它允许用户为给定的数据和时间设置事件。我需要向用户推送通知,所以我需要将他们输入的日期时间和时区转换为东部标准时间,以便我的通知在正确的时间发送出去。



我想在javascript中这样做,所以当数据时间值到达php时,它是正确的格式,然后被添加到数据库。



总而言之,我需要转换一个javascript datatime和时区,通过捕获用户数据时间作为一个完整的UTC日期到我的服务器时区,这是EST - 纽约。



对此事的任何帮助或指示,将不胜感激。
感谢:)

解决方案

请检查此脚本

 函数convertToServerTimeZone(){

// EST
offset = -5.0

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

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

alert(serverDate.toLocaleString());


}


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

解决方案

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


}

这篇关于如何将javascript中的datetime从用户时区转换为EST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆