By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,071 Members | 1,244 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,071 IT Pros & Developers. It's quick & easy.

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

P: 31
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.  
Sep 8 '07 #1
Share this Question
Share on Google+
3 Replies


100+
P: 428
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.
Sep 8 '07 #2

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

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.
Sep 8 '07 #3

pbmods
Expert 5K+
P: 5,821
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 :)
Sep 8 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.