Plain english duration between two dates in javascript

Posted: 30 March 2010 in Uncategorized

I actually have to do it in XSL but I thought I’d get the first version in javascript to see how to do it first.

<script type="text/javascript">
 var date1=new Date("01 January 1970 00:00:00"); // a date in the past
 var date2=new Date(); // now
 var duration = Math.abs(date1.getTime() - date2.getTime());
 // some smart ass is going to say it does not deal with leap years. he'd be right. Talk to the hand :)
 var numYears        =Math.floor(duration            / (365 * 24 * 60 * 60 * 1000));
 var leftOverYears   =           duration            % (365 * 24 * 60 * 60 * 1000) ;
 var numDays         =Math.floor(leftOverYears       / (24 * 60 * 60 * 1000));
 var leftOverDays    =           leftOverYears       % (24 * 60 * 60 * 1000) ;
 var numHours        =Math.floor(leftOverDays        / (     60 * 60 * 1000));
 var leftOverHours   =           leftOverDays        % (     60 * 60 * 1000) ;
 var numMinutes      =Math.floor(leftOverHours       / (          60 * 1000));
 var leftOverMinutes =           leftOverHours       % (          60 * 1000) ;
 var numSeconds      =Math.floor(leftOverMinutes     / (               1000));
 var numMiliSeconds  =           leftOverMinutes     % (               1000) ;

 var durationString=null;
 var yearString="";
 var dayString="";
 var hourString="";
 var minuteString="";
 var secondString="";
 var miliSecondString="";

 if (numYears==1){yearString="1 Year ";} else if (numYears>0){yearString=numYears + " Years "}
 if (numDays==1){dayString="1 Day ";} else if (numDays>0){dayString=numDays + " Days "}
 if (numHours==1){hourString="1 Hour ";} else if (numHours>0){hourString=numHours + " Hours "}
 if (numMinutes==1){minuteString="1 Minute ";} else if (numMinutes>0){minuteString=numMinutes + " Minutes "}
 if (numSeconds==1){secondString="1 Second ";} else if (numSeconds>0){secondString=numSeconds + " Seconds "}
 if (numMiliSeconds==1){miliSecondString="1 MiliSecond ";} else if (numMiliSeconds>0){miliSecondString=numMiliSeconds + " MiliSeconds"}

 durationString = yearString + dayString + hourString + minuteString + secondString + miliSecondString;
 durationString.trim();
 prompt("duration: ",durationString)
 </script>

Tomorrow, the XSL for the same thing.
Advertisements
Comments
  1. Paul Cole says:

    I know you want to do this in XSL but for any date related work in JavaScript you should have a look at DateJS – http://www.datejs.com/

  2. Date manipulation, ninja style! Yeah, I knew about this one, but as you said, it was as a thinking exercise for the XSL (which I am doing right now). I find that the XSL syntax gets in the way of the thinking process a tad…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s