简体   繁体   中英

Get time difference between two date strings

I want to develop a JavaScript function to calculate the activity of users based on the date in the server where the data is stored. The problem is that the date is a string like this:

2013-08-11T20:17:08.468Z

How can I compare two string like this to calculate minor and major time as in the example?

If you want to compare two dates just use this :

var dateA = '2013-08-11T20:17:08.468Z';
var parsedDateA = new Date(dateA).getTime();

var dateB = '2013-06-06T17:33:08.468Z';
var parsedDateB = new Date(dateB).getTime();

if(parsedDateA > parsedDateB) {
    // do something
}

Assuming you need to do the comparisons client-side, the best way is to load the dates into Date objects using Date.parse . Then compare them using the functions provided for Date , such as getTime .

Try parse method:

var s = "2013-08-11T20:17:08.468Z";
var d = Date.parse(s);

As I have understood you in the right way, there is a good answer to your question here .

You can also look at this very good Library ( DateJS ).

If your problem was converting from the Date- String to js- Date look at this Page .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM