How to subtract 2 hours from user's local time?

前端 未结 2 1833
庸人自扰
庸人自扰 2020-12-24 04:19

Can anyone give me a simple JavaScript code block that will allow me to display the local time minus 2 hours?

相关标签:
2条回答
  • 2020-12-24 04:39

    According to Javascript Date Documentation, you can easily do this way:

    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 讨论(0)
  • 2020-12-24 05:00

    Subtract from another date object

    var d = new Date();
    
    d.setHours(d.getHours() - 2);
    
    • Complete reference list for Date object
    0 讨论(0)
提交回复
热议问题