Reputation: 2204
I want to extract the hour, minutes and seconds from the date in variable e
(1:25:40). Then add to the hour in the variable d
("2019-06-29 21:25:38+00").
Expected effect:
21: 25: 38
+5140 seconds
= 22: 51: 18
var d
+ var w
= 22: 51: 18
var d = new Date("2019-06-29 21:25:38+00");
//hour --> 21:25:38
var e = new Date("2019-06-29T 1:25:40.000+00:00");
I want '1: 25: 40' to count as 1h 25 min 40 seconds
1h ---> 3600 seconds
25 min --> 1500 seconds
40 seconds
Result:
21:25:38
+ 1: 25: 40
= 22:51:18
Upvotes: 0
Views: 52
Reputation: 30975
You can simply do
var d = new Date("2019-06-29 21:25:38+00");
d.setSeconds(d.getSeconds() + 5140);
Upvotes: 1
Reputation: 915
Try this: new_d = new Date(d.getTime() + 5140 * 1000)
Given var d = new Date("2019-06-29 21:25:38+00")
, my output is 2019-06-29T22:51:18.000Z
for new_d
.
The reason that this works is because .getTime()
returns a long which represents the number of milliseconds since midnight on Thursday, January 1, 1970 (there's a fun history behind this). You can also pass a Unix-time long into the Date()
constructor to get a new time object, so if you can express your offset in terms of seconds, multiply that value by 1000, add it to your original long, and you're good to go.
Upvotes: 0