【问题标题】:adding and substracting time in JavascriptJavascript中的时间加减法
【发布时间】:2020-05-31 22:25:00
【问题描述】:

我正在尝试使用毫秒为当前时间添加五分钟,但我很困惑为什么加法和减法会给出如此不同的结果:

const now = new Date();
const gimmeFive = now + 300000;
const takeFive = now - 300000;

分别给:

"Sun May 31 2020 23:06:48 GMT+0100 (British Summer Time)300000"

1590962508207

为什么减法有效,加法无效?如何添加时间?


为每个堆栈溢出提示添加了说明: 虽然此处的 Q 与 Add 10 seconds to a Date 重叠,但它在试图理解为什么加法和减法运算符显示不同的行为时有所不同(如 RobG 所解释的) ,为此,非常感谢!)

【问题讨论】:

标签: javascript date time


【解决方案1】:

为什么减法有效,加法无效?如何添加时间?

正如 user120242 在第一条评论中所说,addition operator (+) 被重载,根据使用的值的类型进行算术加法或字符串加法(连接)(请参阅Runtime Semantics: ApplyStringOrNumericBinaryOperator)。

所以在这种情况下:

new Date() + 300000;

日期是第一个converted to a primitive,它返回一个字符串。如果左操作数或右操作数是字符串,则它们都将转换为字符串,然后将两个字符串连接起来。

如果是:

new Date() - 300000;

subtraction operator (-) 将值强制转换为数字,因此将日期转换为其时间值并减去右侧值。

如果您想将 300 秒添加到日期,您可以使用以下方法之一:

let d = new Date();
let ms = 300000;

// Add 3 seconds to the time value, creates a new Date
let e = new Date(d.getTime() + ms)
console.log(e.toString());

// As above but use unary operator + to coerce Date to number
let f = new Date(+d + ms)
console.log(f.toString());

// Use get/setMilliseconds, modifies the Date
d.setMilliseconds(d.getMilliseconds() + ms)
console.log(d.toString());

// Use Date.now
let g = new Date(Date.now() + ms);
console.log(g.toString());

【讨论】:

    【解决方案2】:

    试试这个Date.now()+300000Date.now()-300000

    附:当然,把一切都放在你的常数上

    typeof (new Date)   // returns "object"
    typeof (Date.now()) // returns "number"
    
    /* --------------------------------- //
    SO, of course, you cannot add or subtract numbers from, 
    and to objects. Follow the rules, make calculations with 
    numbers and everything will be OK. 
    */
    
    
    // your code might look like this
    const now = Date.now();
    const gimmeFive = now + 300000;
    const takeFive = now - 300000;
    

    Date.now() 方法返回自 1970 年 1 月 1 日 00:00:00 UTC 以来经过的毫秒数。相反,您可以使用new Date().getTime() - 它还会以毫秒为单位返回一个数字。

    【讨论】:

      【解决方案3】:

      new Date() 返回一个对象。您不应该直接在对象上增加或减少时间。

      相反,您可以使用 Date.now() 返回自 1970 年 1 月 1 日以来经过的当前时间(以毫秒为单位)。

      const now = Date.now();
      const gimmeFive = now + 300000;
      const takeFive = now - 300000;
      
      console.log(new Date(gimmeFive)); // Sun May 31 2020 18:42:20 GMT-0400 (Eastern Daylight Time)
      console.log(new Date(takeFive)); // Sun May 31 2020 18:32:20 GMT-0400 (Eastern Daylight Time)
      

      参考:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

      话虽如此,如果您的项目有可能,我建议使用momentjs。在操纵日期方面,它确实是一个救生员。

      我希望这会有所帮助。干杯。

      【讨论】:

        【解决方案4】:

        如果您使用分钟数,最好的解决方案是使用 Date 的 getMinutessetMinutes 方法。

        var dt = new Date();
        console.log(dt)
        
        dt.setMinutes( dt.getMinutes() + 100 );
        console.log(dt)
        
        dt.setMinutes( dt.getMinutes() - 100 );
        console.log(dt)

        【讨论】:

          【解决方案5】:
          new Date(now.getTime() + 5 * 60000)
          

          因为 new Date 总是返回 Object,

          new Date() - 1 // Return (new Date).getMilliseconds() similar to "1"-1 = 0
          new Date() + 1 // Return string similar to "1"+1 = "11"
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-08-30
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2023-03-23
            相关资源
            最近更新 更多