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!

Javascript Date Question

Status
Not open for further replies.

GhostWolf

Programmer
Jun 27, 2003
290
US
This is my first foray into javascript, and I've hit something I'm not understanding.

Searching for code samples, I found a snippet that calculated the number of years since 1/1/1970 - then modified it to show me the years, months, days, hours, minutes and seconds.

And that's where I hit the problem. Invariably, the result has four extra hours in it! For example, it's now 1600 but the routine will tell me that 20 hours have elapsed in today.

Could this be because of the 4-hour offset from UTC in my time zone? (This happens in IE, Firefox and Opera; haven't tried with any others.) Is there a way to retrieve the system's UTC offset?
.
 
Maybe posting what you've done (actual modified code) will get you a faster response.



--------
GOOGLE is a great resource to find answers to questions like "how do i..."

If you don't know exaclty what you want to do or what to search on, try Google Suggest: --------
I have recently been semi-converted to ensuring all my code (well most of it) works in both javascript and non-javasc
 
You've got it:
Code:
<!-- Time passed since Jan 1st 1970	   -->
var minutes = 1000*60;	   <!-- clock ticks in a minute 			 -->
var hours = minutes*60;	   <!-- in an hour              			 -->
var days = hours*24;	   <!-- in a day                			 -->
var years = days*365;	   <!-- in a year               			 -->

var d = new Date();	   <!-- start with today		  	   		 -->
var t = d.getTime();	   <!-- the # of seconds since 1/1/1970		 -->
var y = t/years;	   <!-- how many years in the whole mess	 -->
document.write("<b>"+d+"</b><p>");
document.write("It's been: " + y + " years since Jan 1st, 1970<p>");  <

<!--  Start fresh 	 -->
var quarter = (years * 4) + days;  <!-- days in four years, (plus leap day)  -->
var Months = Array(); 		   <!-- days in each month	 -->
Months[0] = 31; Months[1] = 28; Months[2] = 31;  Months[3] = 30;
Months[4] = 31; Months[5] = 30; Months[6] = 31;  Months[7] = 31;  
Months[8] = 30; Months[9] = 31; Months[10] = 30; Months[11] = 31;

m = d.getFullYear();	  <!-- get the current year	 -->
v = 1970;
a = 0; x = 0; z = 0;
while (v < m)		  <!-- get accurate leap year count in period-->
	{
	  if ((v % 4) == 0) 
	  {
	   	 a += 1;  	 			  <!-- accumulate leap year count	 -->
		 z += 366;				  <!-- accumulate days in period 	 -->
	  }
	  else
	  {
	   	 z += 365;				  <!-- accumulate days in period 	 -->
	  }
	  v += 1; 					  <!-- increment comparison year	 -->
	  x += 1; 					  <!-- accumulate years in period	 -->
	}
document.write ("It is now "+v+", and there have been ");
document.write (a+" leap years since 1970<br>");

w = t - (z * days);	  <!-- subtract days before 1/1/ThisYear	 -->

<!-- at this point, w = the time elapsed since Jan 1st of this year	and we	 -->
<!-- want to know how many months, days, hours, minutes and seconds	it		 -->
<!-- represents    -->
a = 0; b = 0; n = 0;			  
if ((m % 4)==0) {Months[1]=29;}	  <!-- if it's a leap year     -->
while (w > (Months[a] * days))	  
    {
	  w -= (Months[a] * days);	  <!-- subtract this month's days  			 -->
	  a += 1;		  			  <!-- point to the next month				 -->
	  n += 1;		  			  <!-- accumulate months since Jan 1st		 -->
	}
s = Math.floor(w / days);		  <!-- how many days left in those seconds	 -->
w -= (s * days);				  <!-- subtract remaining days				 -->
r = Math.floor(w / hours);		  <!-- get the number of hours				 -->
w -= (r * hours);  				  <!-- subtract the hours					 -->
q = Math.floor(w / minutes);	  <!-- get the number of minutes			 -->
w -= (q * minutes);				  <!-- and subtract them 					 -->
<!-- finally: the remaining seconds, without the microseconds				 -->
p = Math.floor(w / 1000);					  
<!-- Display the result		 -->
document.write("Or, more precisely:<br>");
document.write(x+" years, "+n+" months, "+s);
if (s==1) {document.write(" day, ");}
else {document.write(" days, ");}
document.write(r+" hours, "+q+" minutes, "+p+" seconds<br>");
document.write("(Those "+x+" years encompass "+z+" days.)<p>");

Yes, I have since found an easier way to initialize the array.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top