Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date modified & DST

Status
Not open for further replies.

craigey

Technical User
Apr 18, 2002
510
GB
Hi,

I have some javascript below that reports the time since the html file was generated. This was all working fine until DST, where it started showing the Last Updated time as an hour behind the actual time.
I believe I understand the concept of javascript time (i.e. milliseconds since 1 Jan 1970), but I just can't seem to apply the DST correction correctly. I did manage to correct part of the code so that it shows the correct time difference between the file being modified & the current time, but the hour is still out on the last updated timestamp. I know I could just add 1 to the hour value, but I was hoping to be able to have the code apply the appropriate timezone value.

JavaScript:
function timer()
{
var lastmod = new Date(document.lastModified);
var lm_day = ('0' + lastmod.getDate()).slice(-2);
var lm_month = ('0' + (lastmod.getMonth()+1)).slice(-2);
var lm_year = lastmod.getFullYear();
var lm_hour = ('0' + lastmod.getHours()).slice(-2);
var lm_minute = ('0' + lastmod.getMinutes()).slice(-2);
var lm_second = ('0' + lastmod.getSeconds()).slice(-2);
var lm_formatted = lm_day+"/"+lm_month+"/"+lm_year+" "+lm_hour+":"+lm_minute+":"+lm_second;
var lm_formatted_for_js = lm_year+"/"+lm_month+"/"+lm_day+" "+lm_hour+":"+lm_minute+":"+lm_second;
var currentDateTime = new Date();
currentTimeIncTimezone = currentDateTime.getTime()+(currentDateTime.getTimezoneOffset()*60*1000);
var nd = new Date(currentTimeIncTimezone);
var day = ('0' + nd.getDate()).slice(-2);
var month = ('0' + (nd.getMonth()+1)).slice(-2);
var year = nd.getFullYear();
var hour = ('0' + nd.getHours()).slice(-2);
var minute = ('0' + nd.getMinutes()).slice(-2);
var second = ('0' + nd.getSeconds()).slice(-2);
var currentTime_Formatted = day+"/"+month+"/"+year+" "+hour+":"+minute+":"+second;
var currentTime_Formatted_for_js = year+"/"+month+"/"+day+" "+hour+":"+minute+":"+second;
var diff = Math.abs(new Date(lm_formatted_for_js) - new Date(currentTime_Formatted_for_js));
var secsago = Math.floor(diff/1000);
var minutes = Math.floor(secsago/60);
var sec_min = ('0' + Math.floor((secsago)%60)).slice(-2);
var min_word = 'minute'
if (minutes != 1) {
var min_word = 'minutes';
}
var lastupdatedstring = "Last updated: "+lm_formatted+" (";
if (minutes != 0 ) {
lastupdatedstring = (lastupdatedstring+minutes+" "+min_word+" ");
} else {
document.getElementById("dt").innerHTML = lastupdatedstring+sec_min+" seconds ago)"
}
window.setTimeout('timer()',1000);
}
 
Is the server Locale set to use DST or is it set to UTC?

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
It's using (UTC) Dublin, Edinburgh, Lisbon, London & it shows DST ends on 30 October at 02:00.
 
I think I've found it. (well it appears to work - will see what happens when the clocks go back)!

Code:
var lm_hour = ('0' + lastmod.getHours()-(lastmod.getTimezoneOffset() /60))).slice(-2);
....
//var currentDateTime = new Date();
//currentTimeIncTimezone = currentDateTime.getTime()+(currentDateTime.getTimezoneOffset()*60*1000);
//var nd = new Date(currentTimeIncTimezone);
var nd = new Date();
.....
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top