如何从用户当地时间中减去2小时?


122

谁能给我一个简单的JavaScript代码块,使我可以显示本地时间减去2小时?

Answers:



32

根据Javascript日期文档,您可以轻松地通过以下方式进行操作:

var twoHoursBefore = new Date();
twoHoursBefore.setHours(twoHoursBefore.getHours() - 2);

而且不用担心您设置的时间是否超出0..23范围。Date()对象将相应地更新日期。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.