Discussion on Addition and Subtraction Time in javascript
- 2021-07-02 23:20:11
- OfStack
In the morning, I encountered 1 problem when dealing with 1 date parameter in js, and I didn't know how to add or subtract time.
The original idea is to take out year, month, day, hour, minute and second by yourself, and then add them manually. . . At first, I wanted to borrow a bit directly. Later, I thought, which is so easy.
Finally, after a morning's groping, it was finally settled.
The first is how to display a standard date and time format "yyyy-mm-dd hh: MM: ss". For the time being,
var dtNow = new Date (). //Current time
The standard date and time are spelled out, and dtNow. getFullYear (), dtNow. getMonth () + 1), dtNow. getDate (), dtNow. toTimeString (). split ("", 1) [0] is the information to be contained in this standard time string.
And the way to add and subtract time is. For example, add N days to the current dtNow
var dtNew = new Date (dtNow. getTime () +N*24*60*60*1000);
Then use dtNew. getFullYear () and so on to get data such as date and time.
Also writing this time would like to overload 1 under the toString method, because the original toString returned a bit rich.. but the format is not good.