Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

compare timestamps in javascript

I have a date saved in a string with this format: 2017-09-28T22:59:02.448804522Z this value is provided by a backend service.

Now, in javascript how can I compare if that timestamp is greater than the current timestamp? I mean, I need to know if that time happened or not yet taking in count the hours and minutes, not only the date.

like image 281
Sredny M Casanova Avatar asked Sep 28 '17 22:09

Sredny M Casanova


People also ask

Can you compare time in JavaScript?

In Javascript comparing date is quite simple. Just create date object of the dates and then using comparison operator you can compare the dates. But this is not that simple when it comes to compare two time strings. Javascript does not provide any direct way to compare time.

How do I compare a timestamp in typescript?

Call the getTime() method on each date to get a timestamp. Compare the timestamp of the dates. If a date's timestamp is greater than another's, then that date comes after.

How do you compare two date objects in JavaScript?

In JavaScript, we can compare two dates by converting them into numeric values to correspond to their time. First, we can convert the Date into a numeric value by using the getTime() function. By converting the given dates into numeric values we can directly compare them.


4 Answers

You can parse it to create an instance of Date and use the built-in comparators:

new Date('2017-09-28T22:59:02.448804522Z') > new Date()
// true
new Date('2017-09-28T22:59:02.448804522Z') < new Date()
// false

Conveniently, the date is already in an standard format that Date knows how to parse (looks like ISO8601).

like image 161
ichigolas Avatar answered Oct 02 '22 13:10

ichigolas


You could also convert it to unix time in milliseconds:

console.log(new Date('2017-09-28T22:59:02.448804522Z').valueOf())

const currentTime = new Date('2017-09-28T22:59:02.448804522Z').valueOf()
    
const expiryTime = new Date('2017-09-29T22:59:02.448804522Z').valueOf()

if (currentTime < expiryTime) {
    console.log('not expired')
}
like image 23
agm1984 Avatar answered Oct 02 '22 13:10

agm1984


const anyTime = new Date("2017-09-28T22:59:02.448804522Z").getTime();
const currentTime = new Date().getTime();
if(currentTime > anyTime){
    //codes
}
like image 41
samet Avatar answered Oct 02 '22 13:10

samet


If you can, I would use moment.js * https://momentjs.com/

You can create a moment, specifying the exact format of your string, such as:

var saveDate = moment("2010-01-01T05:06:07", moment.ISO_8601);

Then, if you want to know if the saveDate is in the past:

boolean isPast = (now.diff(saveDate) > 0);

If you can't include an external library, you will have to string parse out the year, day, month, hours, etc - then do the math manually to convert to milliseconds. Then using Date object, you can get the milliseconds:

var d = new Date();
var currentMilliseconds = d.getMilliseconds();

At that point you can compare your milliseconds to the currentMilliseconds. If currenMilliseconds is greater, then the saveDate was in the past.

like image 36
TinkerTenorSoftwareGuy Avatar answered Oct 02 '22 12:10

TinkerTenorSoftwareGuy