Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculate time elapsed using javascript

Tags:

javascript

I want to calculate time elapsed since my birthday in the form of (years, months, days, hours, minutes, seconds) using JavaScript.

For example, my birth date is 15-Oct-1989, 00 hrs 00 mins 00 secs. Hence, time elapsed since my birth date is,

22 years 5 months 10 days 19 hours 25 minutes 25 seconds 

I want to achieve the same output using JavaScript code. Any link or so will certainly help in this case.

like image 513
Pankaj Parashar Avatar asked Mar 25 '12 14:03

Pankaj Parashar


4 Answers

Try something like this:

var now = new Date();
var bDay = new Date(1989, 10, 15);
var elapsedT = now - bDay; // in ms

Read MDN for further info. That'll give you some idea how to format the result.

like image 158
Juho Vepsäläinen Avatar answered Sep 28 '22 14:09

Juho Vepsäläinen


Since my previous answer has people missing the point entirely, here's a port of PHP code I have to do the same thing:

function getDaysInMonth(month,year) {     
    if( typeof year == "undefined") year = 1999; // any non-leap-year works as default     
    var currmon = new Date(year,month),     
        nextmon = new Date(year,month+1);
    return Math.floor((nextmon.getTime()-currmon.getTime())/(24*3600*1000));
} 
function getDateTimeSince(target) { // target should be a Date object
    var now = new Date(), diff, yd, md, dd, hd, nd, sd, out = [];
    diff = Math.floor(now.getTime()-target.getTime()/1000);
    yd = target.getFullYear()-now.getFullYear();
    md = target.getMonth()-now.getMonth();
    dd = target.getDate()-now.getDate();
    hd = target.getHours()-now.getHours();
    nd = target.getMinutes()-now.getMinutes();
    sd = target.getSeconds()-now.getSeconds();
    if( md < 0) {yd--; md += 12;}
    if( dd < 0) {
        md--;
        dd += getDaysInMonth(now.getMonth()-1,now.getFullYear());
    }
    if( hd < 0) {dd--; hd += 24;}
    if( md < 0) {hd--; md += 60;}
    if( sd < 0) {md--; sd += 60;}

    if( yd > 0) out.push( yd+" year"+(yd == 1 ? "" : "s"));
    if( md > 0) out.push( md+" month"+(md == 1 ? "" : "s"));
    if( dd > 0) out.push( dd+" day"+(dd == 1 ? "" : "s"));
    if( hd > 0) out.push( hd+" hour"+(hd == 1 ? "" : "s"));
    if( nd > 0) out.push( nd+" minute"+(nd == 1 ? "" : "s"));
    if( sd > 0) out.push( sd+" second"+(sd == 1 ? "" : "s"));
    return out.join(" ");
}

Example:

getDateTimeSince(new Date(1992,1,6,22,30,00)); 
// my date of birth - near enough half past ten in the evening on Feb 6th 1992
> 20 years 1 month 18 days 17 hours 23 minutes 7 seconds

I believe this is exactly what the OP was asking for.

like image 34
Niet the Dark Absol Avatar answered Sep 28 '22 12:09

Niet the Dark Absol


First of all, what you demand is a bit imprecise. We know that a minute = 60 seconds, a hour = 60 minutes... And it stops here. A day can be either 24 or just a little more than 24 hours, depending how you treat leap years, and "one month" doesn't even try to represent a time span precisely.

Hence: Either keep your timespans as hours, or establish an approximation to deal with leap years etc. Dates and date differences (timespans) are different concepts and need to always be treated differently.

Anyway, as for the code, I'd simply go for :

var ms = new Date() - yourBirthDate;
var secs = ms/1000;

var minutes = secs    / 60 ;  secs    = secs    % 60;
var hours   = minutes / 60 ;  minutes = minutes % 60;
// and so on if you want
like image 37
Kos Avatar answered Sep 28 '22 14:09

Kos


Use Moment.js for parsing, validating, manipulating, and formatting dates in Javascript. Only 5.5k so not much of a good reason to do this kind of low-level code yourself these days.

http://momentjs.com/

like image 42
Obie Avatar answered Sep 28 '22 12:09

Obie