www.webdeveloper.com
Results 1 to 10 of 10

Thread: Javascript date calculation

Hybrid View

  1. #1
    Join Date
    Sep 2004
    Posts
    24

    Javascript date calculation

    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 dt1 = Date.parse(x)
    var dt2 = Date.parse(y)


    var ga = Math.round(((280 - (dt1.valueOf() - dt2.valueOf()) / (60 * 60 * 24 * 1000)) / 7) * Math.pow(10, 1)) / Math.pow(10, 1);

    if (inStr(ga,".") = -1) {
    document.getElementById('txtGaAdmit').value=ga + ".0"
    }
    else {
    if (ga.subStr(inStr(ga,".")+1,1) >= 7) {
    document.getElementById('txtGaAdmit').value = (ga.subStr(0, inStr(ga, ".") - 1) + 1) + ".0"
    }
    else {
    document.getElementById('txtGaAdmit').value=ga
    }
    }

  2. #2
    Join Date
    Feb 2006
    Posts
    2,927
    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))

  3. #3
    Join Date
    Nov 2002
    Location
    Baltimore, Maryland
    Posts
    12,270
    Quote Originally Posted by mrhoo View Post
    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

  4. #4
    Join Date
    Feb 2006
    Posts
    2,927
    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.
    Last edited by mrhoo; 05-29-2009 at 12:15 AM.

  5. #5
    Join Date
    Nov 2002
    Location
    Baltimore, Maryland
    Posts
    12,270
    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

  6. #6
    Join Date
    Nov 2002
    Location
    Baltimore, Maryland
    Posts
    12,270
    By way of example, run the following:
    Code:
    <!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

  7. #7
    Join Date
    Sep 2004
    Posts
    24
    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.

    jb

  8. #8
    Join Date
    Mar 2005
    Location
    Sydney, Australia
    Posts
    7,974
    Math.floor(remainder + .3)
    Stephen

  9. #9
    Join Date
    Sep 2004
    Posts
    24
    Stephen,

    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.

    Janet

  10. #10
    Join Date
    Mar 2005
    Location
    Sydney, Australia
    Posts
    7,974
    So you want if it is less than .7 to leave it as is and round up only when the tenths are > .7.

    In that case:

    remainder = ((remainder - Math.floor(remainder)) > .7) ? Math.floor(remainder + .3).toFixed(1) : remainder;
    Stephen

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles