Adding minutes to a javascript Date in Node.js - Stack Overflow

I'm having trouble adding minutes to a date in javascript in Node.js. I have a date object, bt_tim

I'm having trouble adding minutes to a date in javascript in Node.js. I have a date object, bt_time = new Date()

bt_time.toString()
"Mon Mar 07 2016 03:30:10 GMT+0000 (UTC)"

The following operations attempting to add 5 minutes to the give the following results

bt_time + (60*1000*5)
"Mon Mar 07 2016 03:30:10 GMT+0000 (UTC)300000"

new Date(bt_time + (60*1000*5)).toString()
"Mon Mar 07 2016 03:30:10 GMT+0000 (UTC)"

new Date(bt_time) + (60*1000*5)
"Mon Mar 07 2016 03:30:10 GMT+0000 (UTC)300000"

It seems like the + (60*1000*5) is just tacking 300000 at the end of the date string, instead of adding to the time. I don't have the same issue when I attempt subtraction.

I need the date arithmetic to be able to iterate spans of days, 5 minutes at a time.

I'm having trouble adding minutes to a date in javascript in Node.js. I have a date object, bt_time = new Date()

bt_time.toString()
"Mon Mar 07 2016 03:30:10 GMT+0000 (UTC)"

The following operations attempting to add 5 minutes to the give the following results

bt_time + (60*1000*5)
"Mon Mar 07 2016 03:30:10 GMT+0000 (UTC)300000"

new Date(bt_time + (60*1000*5)).toString()
"Mon Mar 07 2016 03:30:10 GMT+0000 (UTC)"

new Date(bt_time) + (60*1000*5)
"Mon Mar 07 2016 03:30:10 GMT+0000 (UTC)300000"

It seems like the + (60*1000*5) is just tacking 300000 at the end of the date string, instead of adding to the time. I don't have the same issue when I attempt subtraction.

I need the date arithmetic to be able to iterate spans of days, 5 minutes at a time.

Share Improve this question asked Mar 21, 2016 at 3:57 Zain SyedZain Syed 4834 silver badges13 bronze badges 3
  • 2 How about bt_time.setMinutes(bt_time.getMinutes() + 5); – Pointy Commented Mar 21, 2016 at 3:58
  • @Pointy, will that be able to advance the date to the next hour, at the top of the hour? Or advance to the next day beyond 23:59 – Zain Syed Commented Mar 21, 2016 at 4:00
  • 2 Yes, it will. Adding to any of the Date fields will result in a proper date reflecting the difference. – Pointy Commented Mar 21, 2016 at 4:03
Add a ment  | 

2 Answers 2

Reset to default 4

Re:

I don't have the same issue when I attempt subtraction

because the subtraction operator - forces it's operands to Number, so:

bt_time - (60*1000*5)

is effectively:

bt_time.getTime() - 300000

which will create a number (that represents milliseconds since the ECMAScript epoch) with a value 300,000 smaller than the time value of bt_time.

Already answered, but for pleteness:

However, the addition operator + is overloaded, so in:

bt_time + (60*1000*5)

the script engine has to work out whether it means addition, concatenation or coercion to a number (+ unary operator). By default, a Date object coerces to string, so + acts as a concatenation operator and as Daishi Nakajima says, is effectively:

bt_time.toString() +  300000

bt_time is object type. bt_time + i means bt_time.toString() + i

correct is

new Date(bt_time.getTime() + 60*1000*5).toString();
// or
bt_time.setMinutes(bt_time.getMinutes() + 5);

I remend moment.js in nodejs

moment().add(5, 'minutes').toDate();

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745646611a4638008.html

相关推荐

  • Adding minutes to a javascript Date in Node.js - Stack Overflow

    I'm having trouble adding minutes to a date in javascript in Node.js. I have a date object, bt_tim

    5小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信