#### Hybrid View

1. Registered User
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 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 {
}
}

2. Registered User+
Join Date
Feb 2006
Posts
2,930
var d1=Date.parse('5/21/2009');
var d2=Date.parse('5/28/2009');

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

3. 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.

4. Registered User+
Join Date
Feb 2006
Posts
2,930
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. Yes, but you are then calculating UTC weeks, not real weeks. Real weeks aren't all of the same number of ticks.

6. 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">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Example</title>
<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.

7. Registered User
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. Math.floor(remainder + .3)

9. Registered User
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. 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;

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

#### Posting Permissions

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

"

"

## X vBulletin 4.2.2 Debug Information

• Page Generation 0.15161 seconds
• Memory Usage 3,206KB
• Queries Executed 13 (?)
Template Usage (31):
• (1)bbcode_code
• (1)bbcode_quote
• (1)footer
• (1)forumjump
• (1)forumrules
• (1)gobutton
• (10)memberaction_dropdown
• (1)navbar
• (1)navbar_moderation
• (1)navbar_noticebit
• (1)navbar_tabs
• (2)option
• (10)postbit
• (10)postbit_onlinestatus
• (10)postbit_wrapper
• (1)spacer_close
• (1)spacer_open
• (1)tagbit_wrapper

Phrase Groups Available (6):
• global
• inlinemod
• postbit
• posting
• reputationlevel
Included Files (27):
• ./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/functions_bigthree.php
• ./includes/class_postbit.php
• ./includes/class_bbcode.php
• ./includes/functions_reputation.php
• ./includes/functions_notice.php
• ./packages/vbattach/attach.php
• ./vb/types.php
• ./vb/cache.php
• ./vb/cache/db.php
• ./vb/cache/observer/db.php
• ./vb/cache/observer.php

Hooks Called (72):
• init_startup
• friendlyurl_resolve_class
• 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
• strip_bbcode
• friendlyurl_clean_fragment
• friendlyurl_geturl
• forumjump
• cache_templates
• cache_templates_process
• template_register_var
• template_render_output
• fetch_template_start
• fetch_template_complete
• parse_templates
• notices_check_start
• notices_noticebit
• process_templates_complete
• friendlyurl_redirect_canonical
• bbcode_fetch_tags
• bbcode_create
• postbit_factory
• postbit_display_start
• postbit_imicons
• bbcode_parse_start
• bbcode_parse_complete_precache
• bbcode_parse_complete
• postbit_display_complete
• memberaction_dropdown
• tag_fetchbit
• tag_fetchbit_complete
• forumrules
• navbits
• navbits_complete