views:

65

answers:

3

I want to get the time difference between saved time and current time in javascript or jquery. My saved time looks like Sun Oct 24 15:55:56 GMT+05:30 2010.

The date format code in java looks like

String newDate = "2010/10/24 15:55:56";
DateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = format.parse(newDate);

How to compare it with the current time and get the difference? Is there any inbuilt function in jquery or javascript??

Any suggestions or links would be appreciative!!!

Thanks in Advance!

Update

Date is stored as varchar in the DB. I am retriving it to a String variable and then change it to java.util.Date object. The java code looks like

String newDate = "2010/10/24 15:55:56";
DateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = format.parse(newDate);

This date object was sent to client. There i want to compare the saved date with current date and want to show the time difference like 2 secs ago, 2 hours ago, 2 days ago etc... like exactly in facebook. I have gone through some date to timestamp conversion tutorial in java script and now i can get the difference in timestamp. Now, i want to know how i shall change it to some format like "2 secs or 2 days or 24 hours"??. Or, how i shall change it back to date format???

+1  A: 

jQuery doesn't add anything for working with dates. I'd recommend using Datejs in the event that the standard JavaScript Date API isn't sufficient.

Perhaps you could clarify exactly what input and output you're aiming for. What do you mean by "the difference?" There is more than one way to express the difference between to instants in time (primarily units and output string formatting).


Edit: since you said you're working with jQuery, how about using CuteTime? (Demo page)

Matt Ball
@Matt Ball: I have updated my question.
NooBDevelopeR
@NooB: see my edit.
Matt Ball
@Matt Ball: Thanks. This is what exactly i needed.
NooBDevelopeR
@NooB: you're welcome.
Matt Ball
+4  A: 

Convert them into timestamps which are actually integers and can get subtracted from each other. The you just have to convert back the resulting timestamp to a javascript date object.

var diff = new Date();
diff.setTime( time2.getTime()-time1.getTime() );
joni
I'd add to that, if the difference is greater than a day, you'll need to do some additional processing to work out the number of days.
Spudley
@Joni: Thanks for your answer. I have changed both of the time's to timestamp and got the difference. Now, how i can change it to date??
NooBDevelopeR
@Joni: I have updated my question and explained more about my problem. Pls, have a look at it.
NooBDevelopeR
@NooBDevelopeR the simplest solution is to let java convert the timestamp to a readable format like 2010/10/24 15:55:56. But to achieve what you want ("2 secs or 2 days or 24 hours") you will have to build yourself a format function with ifs or case select, e.G. (pseudocode) ` if time < 60: return (time+"seconds"); if time < 3600: return((time/60)+"mins") ` etc.
joni
+1  A: 

You dont need to explicit convert, just do this:

var timediff = new Date() - savedTime;

This will return the difference in milliseconds.

madeinstefano