如何使用Javascript Date对象计算东方时间? [英] How to calculate eastern time using Javascript Date object?

查看:122
本文介绍了如何使用Javascript Date对象计算东方时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开展涉及Javascript的个人项目,作为该项目的一部分,我想抓住当前日期(包括时间)并相应地显示它。没什么大不了对的那么交易是,我想返回时间&日期在 Eastern Daylight TIme 中,无论世界哪里知道IP。

I'm working on a personal project involving Javascript, and as part of that project, I want to grab the current date (including time) and display it accordingly. No big deal right? Well, the deal is that I want to return the time & date in Eastern Daylight TIme, no matter where in the world the IP is.

如果这不可能,你建议哪些替代方法? php有这个功能吗?我可以编写一个简单的php脚本,该脚本需要一个日期并进行转换,但是如果可能的话我想保留在JS中。

If this is not possible, what alternative methods do you suggest? Does php have this functionality? I could write a simple php script that takes a date and converts it, but I want to keep this in JS if at all possible.

我想通过最好的方式来思考,但我可以感谢您提供的任何帮助。

I'm trying to think through the best way to do this, but I'd appreciate any help that you could offer.

谢谢!

推荐答案

JavaScript本机日期只知道两个时区,UTC和用户的区域设置时区(即使这样,您可以提取关于区域设置时区的信息量有限)。你可以在UTC工作,减去4个小时以获得EDT,但是你真的总是想要EDT而不是EST?

JavaScript native Date objects only know two timezones, UTC and the user's locale timezone (and even then, the amount of information you can extract about the locale timezone is limited). You could work in UTC and subtract 4 hours to get EDT, but do you really always want EDT and not EST?

如果你想在任意地区之间进行时区转换PHP需要使用自己的时区信息(例如 TimezoneJS 拖入大型图书馆。

If you want to do timezone conversions between arbitrary regions in PHP you'll need to drag in a large library with its own timezone information, such as TimezoneJS.

最好将JavaScript全部保存在UTC中,让PHP侧面担心会对特定的区域设置/时区进行格式化,例如使用时区内的东西日期/时间

It may be better to keep the JavaScript stuff all in UTC, and let the PHP side worry about formatting it for a particular locale/timezone, using eg the timezone stuff from Date/Time.

这篇关于如何使用Javascript Date对象计算东方时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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