473,386 Members | 1,817 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

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
3 3307
mrhoo
428 256MB
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
Gretsch
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
5,821 Expert 4TB
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

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

Similar topics

4
by: dave | last post by:
Hello, Trying to get a php script to display when a web page was last modified. My code is below, i'm getting a last modified date of dec. 31 1969 which is wrong for one, and does not change if i...
6
by: Ajay | last post by:
hi! i am reading a file on the Web. How can i find out when it was last modified? thanks
2
by: Terry | last post by:
I have used the code provided in the PreciseDateDiff function at the following Access Web link to calculate the time change date (from Daylight to Standard time and vice versa): ...
2
by: Gershon | last post by:
I have a simple table including First Name, Last Name, Start Date. I need to create a report listing First, Last and the number of days since Start Date. The First and Last names are easy. How do I...
1
by: untiroalaire | last post by:
Hi, here's what I'd like to do: get the time difference (Elapsed) between the "received date" field for all emails (running total) I've dumped into access from outlook and summarize at the...
8
by: Philipp Buehler | last post by:
Hello, postgresql 7.3.4 on Debian or the redhat packaged 7.3.4-8 on RHEL AS3 - same issue, so I somewhat cut out RH is playing things on me. Tested on two different PCs, too (say, one debian,...
3
by: rdudejr | last post by:
Hi all, Ive got a database approx 350 GB in which Im getting very high Time waited for prefetch. This is directly out of the snapshot for the db (these are for the entire database I assume as I...
33
by: ram.ragu | last post by:
hi i have problem to calculate idle time of cpu and if idle time is more then i have to shut down the system. can anyone tell me the idea to so that please
30
by: Barry L. Bond | last post by:
Greetings! I just got a new Peet Brothers Ultimeter 2100 Weather Station. This new one has a way to display the heat index, if you press the "dew point" key twice. Being aware of all the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.