StackerSapper
StackerSapper

Reputation: 251

javascript add 1ms to a string date

I have a var in js which is represented like so: "lastTimeModified": "2019-02-26T11:38:20.222Z"

and I want to add to it 1ms

I have tried something like this:

dateObj = new Date();
dateObj.setMilliseconds(1);
var newDate = lastTimeModified + dateObj

but that doesn't seem to work.

Upvotes: -1

Views: 1612

Answers (2)

lusc
lusc

Reputation: 1406

The below takes your time string turns it into a date object, gets the milliseconds since the 1. of January 1970 adds 1ms and turns it back into a date object

const newDate = new Date(new Date(lastTimeModified).getTime() + 1)

Upvotes: 1

Ankur Parihar
Ankur Parihar

Reputation: 495

If you can get your date in milliseconds than you can directly add 1 millisecond to it

var date = new Date(lastTimeModified)    // convert string to date object
var mill = date.getMilliseconds()        // Get millisecond value from date
mill += 1                                // Add your one millisecond to it
date.setMilliseconds(mill)               // convert millisecond to again date object

Upvotes: 5

Related Questions