# Javascript date calculation

• 05-28-2009, 10:19 AM
janetb
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 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) {
}
else {
if (ga.subStr(inStr(ga,".")+1,1) >= 7) {
document.getElementById('txtGaAdmit').value = (ga.subStr(0, inStr(ga, ".") - 1) + 1) + ".0"
}
else {
}
}
• 05-28-2009, 12:37 PM
mrhoo
var d1=Date.parse('5/21/2009');
var d2=Date.parse('5/28/2009');

var weeks= Math.abs((d1-d2)/6.048e8);

• 05-28-2009, 02:03 PM
Charles
Quote:

Originally Posted by mrhoo
var d1=Date.parse('5/21/2009');
var d2=Date.parse('5/28/2009');

var weeks= Math.abs((d1-d2)/6.048e8);

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.
• 05-29-2009, 12:08 AM
mrhoo
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.
• 05-29-2009, 04:14 AM
Charles
Yes, but you are then calculating UTC weeks, not real weeks. Real weeks aren't all of the same number of ticks.
• 05-29-2009, 08:20 AM
Charles
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.
• 05-29-2009, 01:26 PM
janetb
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
• 05-31-2009, 02:49 PM
felgall
Math.floor(remainder + .3)
• 06-01-2009, 09:02 AM
janetb
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
• 06-01-2009, 09:43 PM
felgall
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;

## X vBulletin 4.2.2 Debug Information

• Page Generation 0.13453 seconds
• Memory Usage 2,373KB
• Queries Executed 11 (?)
Template Usage (21):
• (1)bbcode_code_printable
• (1)bbcode_quote_printable
• (1)footer
• (1)gobutton
• (1)navbar_moderation
• (1)navbar_noticebit
• (2)option
• (1)spacer_close
• (1)spacer_open

Phrase Groups Available (3):
• global
• postbit
Included Files (19):
• ./global.php
• ./includes/class_bootstrap.php
• ./includes/init.php
• ./includes/class_core.php
• ./includes/config.php
• ./includes/functions.php
• ./includes/class_friendly_url.php
• ./includes/class_hook.php
• ./includes/class_bootstrap_framework.php
• ./vb/vb.php
• ./vb/phrase.php
• ./includes/functions_calendar.php
• ./includes/class_bbcode_alt.php
• ./includes/class_bbcode.php
• ./includes/functions_bigthree.php
• ./includes/functions_notice.php

Hooks Called (41):
• init_startup
• init_startup_session_setup_start
• database_pre_fetch_array
• database_post_fetch_array
• init_startup_session_setup_complete
• global_bootstrap_init_start
• global_bootstrap_init_complete
• cache_permissions
• fetch_foruminfo
• global_state_check
• global_bootstrap_complete
• global_start
• style_fetch
• global_setup_complete
• bbcode_fetch_tags
• bbcode_create
• bbcode_parse_start
• bbcode_parse_complete_precache
• bbcode_parse_complete
• cache_templates
• cache_templates_process
• template_register_var
• template_render_output
• fetch_template_start
• fetch_template_complete
• parse_templates