I was playing around with the native Date object, and I realised that subtraction works (returns an integer representing the total number of miliseconds between the two dates), but addition simply combines them via String concatenation. Similarly, adding an integer to a date (e.g. I want to get the time that is 3000ms later) will return a string, while subtracting an integer to a date works.
Just wondering if this is expected behaviour and whether I'm missing something?
You can simply convert current date into milliseconds, add your desired increment then convert it back to date time format and there you have your incremented date:
var d = new Date(); alert("Current DateTime: " + d); var milliseconds = d.getTime(); //this will convert current date into milliseconds.. //Now youw want to progress the date by 3000ms.. simply add it to the current date time.. milliseconds += 3000; d = new Date(milliseconds); //your new incremented date alert("After 3000ms: " + d);
+ operator is overloaded for strings,
- is not.
You need to use
var newVal=parseInt(new Date().getTime()) + 1000
©2020 All rights reserved.