Stefano Maglione
Stefano Maglione

Reputation: 4150

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?

Upvotes: 0

Views: 908

Answers (4)

m_vdbeek
m_vdbeek

Reputation: 3774

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
}

Upvotes: 1

Martin
Martin

Reputation: 3286

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.

Upvotes: 0

rook
rook

Reputation: 6240

Try parse method:

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

Upvotes: 0

Justin Ethier
Justin Ethier

Reputation: 134167

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.

Upvotes: 0

Related Questions