Javascript 如何从用户的本地时间中减去 2 小时?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4943088/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to subtract 2 hours from user's local time?
提问by Etienne
Can anyone give me a simple JavaScript code block that will allow me to display the local time minus 2 hours?
谁能给我一个简单的 JavaScript 代码块,让我显示本地时间减去 2 小时?
回答by BrunoLM
Subtract from another date object
从另一个日期对象中减去
var d = new Date();
d.setHours(d.getHours() - 2);
回答by Kostanos
According to Javascript Date Documentation, you can easily do this way:
根据Javascript Date Documentation,您可以轻松地这样做:
var twoHoursBefore = new Date();
twoHoursBefore.setHours(twoHoursBefore.getHours() - 2);
And don't worry about if hours you set will be out of 0..23
range.
Date() object will update the date accordingly.
不要担心您设置的小时数是否超出0..23
范围。Date() 对象将相应地更新日期。