andkjaer
andkjaer

Reputation: 4065

Javascript date and time comparisson

How can I compare these two date and time values:

date 1 = 2012-03-07 11:55:18

date 2 = 2012-01-02 11:02:44

and find the time difference between them?

Upvotes: 1

Views: 151

Answers (3)

Balaswamy Vaddeman
Balaswamy Vaddeman

Reputation: 8530

you can get seconds difference with below code,you can convert that accordingly

var date1=new Date("2012-03-07 11:55:18");
var date2=new Date("2012-01-02 11:02:44");
var mSeconds=date1.getTime()-date2.getTime();
alert(mSeconds/1000);​

jsfiddle example

Upvotes: 2

H.Rabiee
H.Rabiee

Reputation: 4837

You can create the two dates with the Javascript Date object and then compare them with the usual operators.

See W3 Schools Example

Upvotes: 0

Saket Patel
Saket Patel

Reputation: 6683

you can convert the date objects to timestamps like date1.getTime() and then use those timestamps to get difference in milliseconds

Upvotes: 0

Related Questions