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.
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);
}