Connecting Tech Pros Worldwide Forums | Help | Site Map

How to calculation time elapsed since this web page was last modified.

Newbie
 
Join Date: Sep 2007
Posts: 31
#1: Sep 8 '07
Web, html, javascript,

Hi,
I need to calculate the time since this .htm file was last modified. {which I can then use in a calculation, rather than display, so days&decimals format would be OK}
I can get the time NOW, but I am having trouble understanding the format/content of the document.lastModified field.
Any help would be much appreciated - thanks

I have a function to calculate the difference between the 2 dates, but when i run it I get: Object doesn't support this property or method


Expand|Select|Wrap|Line Numbers
  1. <script language="JavaScript" type="text/javascript">
  2. <!--
  3. function days_between(date1, date2) {                //calculates day & fractions of days between 2 dates_times
  4.  
  5.     var ONE_DAY = 1000 * 60 * 60 * 24                // Nb The number of milliseconds in one day
  6.     // Convert both dates to milliseconds
  7.     var date1_ms = date1.getTime()
  8.     var date2_ms = date2.getTime()
  9.  
  10.     // Calculate the difference in milliseconds
  11.     var difference_ms = Math.abs(date1_ms - date2_ms)
  12.  
  13.     // Convert back to days&decimals and return
  14. //    return Math.round(difference_ms/ONE_DAY)     // forget about rounding
  15.       return (difference_ms/ONE_DAY)
  16. }
  17.  
  18. //-->
  19. </script>
  20.  
And to get the 2 dates I am using:

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">       
  2.   <!--
  3.   var current_date = new Date()
  4.  
  5.   document.writeln("Current time: " + current_date + "<p />")    // print for testing
  6.  
  7.   lastmod = document.lastModified     // get string of last modified date
  8.   lastmoddate = Date.parse(lastmod)   // convert modified string to date
  9.   if(lastmoddate == 0){               // unknown date (or January 1, 1970 GMT)
  10.      document.writeln("Last Modified: Unknown")
  11.      } else {
  12.      document.writeln("Last Modified: " + lastmod + "<p />")
  13.  
  14. // Call the days_between function
  15.   var time_elapsed = days_between(current_date, lastmod )
  16. // and for testing just print it for now..
  17.   document.writeln("Time  elapsed =: " + time_elapsed + "<p />")
  18. }
  19. //-->
  20. </script>
  21.  

Needs Regular Fix
 
Join Date: Jun 2006
Posts: 424
#2: Sep 8 '07

re: How to calculation time elapsed since this web page was last modified.


Date.parse returns an integer- the time stamp. Use it to create a Date object:


var lastmod = new Date(Date.parse(document.lastModified))

Then subtract lastmod from new Date(), and use that integer (milliseconds difference) to calculate the difference in days, hours etc.
Newbie
 
Join Date: Sep 2007
Posts: 31
#3: Sep 8 '07

re: How to calculation time elapsed since this web page was last modified.


Brilliant,
It now works.
Thanks very much for your help - and so quick !!

Quote:

Originally Posted by mrhoo

Date.parse returns an integer- the time stamp. Use it to create a Date object:


var lastmod = new Date(Date.parse(document.lastModified))

Then subtract lastmod from new Date(), and use that integer (milliseconds difference) to calculate the difference in days, hours etc.

pbmods's Avatar
Site Moderator
 
Join Date: Apr 2007
Location: Texas
Posts: 5,435
#4: Sep 8 '07

re: How to calculation time elapsed since this web page was last modified.


Heya, Gretsch. Welcome to TSDN!

Please use CODE tags when posting source code:

[CODE=javascript]
JavaScript code goes here.
[/CODE]

Glad to hear you got it working! Good luck with your project, and if you ever need anything, post back anytime :)
Reply