I'm not a Javascript guru and would appreciate some help. Basically, I'm taking the difference between two dates and then displaying the numbers of weeks with a remainder. If the first remainder decimal is 7 or greater, they want the number rounded up to the next whole number. If it's a whole number, just tack on a ".0" for consistency. I've tried the following, but it doesn't seem to work. Can anybody help or is there a cleaner way to do it?
var x = document.getElementById('txtDueDate').value;
var y = document.getElementById('txtAdmit').value;
var d1=Date.parse('5/21/2009');
var d2=Date.parse('5/28/2009');
var weeks= Math.abs((d1-d2)/6.048e8);
alert(weeks.toFixed(1))
Sort of. Thanks to the wonder that is Daylight Time you can't accurately reckon durations that long. To do this right you'll have to cycle through d.setDate (d.getDate() + 7) and count them up.
“The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect.”
—Tim Berners-Lee, W3C Director and inventor of the World Wide Web
var d1= Date.parse('5/21/2009');
var d2= Date.parse('5/28/2009');
Date.parse does not return a Date object, localized to the user, but a genuine timestamp.
The number of milliseconds between January 1, 1970 at 12:00:00 (AM) in Greenwich, England and the date you are parsing.
This is like the return from a Date object's getTime() method- and why there is no need for getUTCTime.
As long as you do not force a local Date object, there is no daylight savings time, and each day has 24 hours.
Yes, but you are then calculating UTC weeks, not real weeks. Real weeks aren't all of the same number of ticks.
“The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect.”
—Tim Berners-Lee, W3C Director and inventor of the World Wide Web
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Example</title>
</head>
<body>
<ul>
<script type="text/javascript">
d = new Date('1 Jan 2009');
while (d < new Date ('1 Jan 2010')) {
t1 = d.getTime();
d.setDate (d.getDate() + 7);
document.write ('<li>', d - t1);
}
</script>
</ul>
</body>
</html>
You'll notice that twice a year a week isn't 604,800,000 ticks long.
“The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect.”
—Tim Berners-Lee, W3C Director and inventor of the World Wide Web
Great folks - thanks for everything. I guess I really won't have to worry about too much - it's never going to be more than 40-45 weeks because the dates being calculated are gestation weeks for pregnancy.
But, back to the main question. They want me to round up to the next whole number anything greater than or equal to .7 in the remainer. I know how to do that on the asp.net side, but I'm not really good at JavaScript and having trouble on that side. Any help there? I'd really appreciate it.
Thanks so much for your reply - I appreciate it. Problem is, that's not what they want. If I simply add three tenths, the number itself changes. They literally want me to evaluate the tenth to see if it's greater than or equal to 7 and only then increment it to the next whole number and tack on a ".0". Since I'm not very good at JavaScript, I can't tell if my problem is with evaluating to see if the tenth is greater than or equal to 7, or trying to increment the number.
Bookmarks