making the datediff function round down

# Thread: making the datediff function round down

1. Member
Join Date
Dec 1969
Posts
45

## making the datediff function round down

is there a way to make the datediff function round down instead of up when it comes to a partial interval? for example, if the interval is weeks, it will round the number of weeks up, which is throwing off some calculations i&#039;m trying to do horribly. is there any way to fix that?

2. Senior Member
Join Date
Dec 1969
Location
Los Angeles, CA
Posts
21,192

## Say that again

and this time with an example<BR><BR><BR>

3. Member
Join Date
Dec 1969
Posts
45

## again.

example. datediff("ww","4/10/2002","4/21/2002") returns 2, when it is not, in fact, 2 weeks... its only 11 days, not 14. how can i make the function return 1 (in that situation) instead of incorrectly rounding the number up. or maybe even return the exact number? (0.785714....)

4. Member
Join Date
Dec 1969
Posts
45

## oops

my math was wrong... but you get the idea.

5. Senior Member
Join Date
Dec 1969
Location
Los Angeles, CA
Posts
21,192

## You cant

i think it counts the # of Sundays between the 2 days<BR><BR><BR>the only way will be a workaround.....get the # od days then devide that by 7 and you will get your diff<BR><BR>

6. Senior Member
Join Date
Dec 1969
Posts
96,118

## And to round down...

...instead of using the ordinary divide by 7, use the VBScript backslash operator:<BR><BR>To use the chosen example:<BR><BR>numdays = datediff("d","4/10/2002","4/21/2002") <BR>&#039; now numdays is 11<BR>numWholeWeeks = numdays 7 &#039; numWholeWeeks will be ONE!<BR><BR>(If you use / to divide, you will get 1.571428... weeks. Which you could turn into one week using the INT function, but the "integer divide" operator--the backslash--does it all in one step.)<BR><BR>

7. Member
Join Date
Dec 1969
Posts
45

## well i went ahead and did it by myself...

all this has been about me trying to create something that counts exactly how long my site has been up... so here&#039;s what i concocted:<BR><BR>years=datediff("yyyy","2/12/2002 10:58:59 AM",Date() & " " & Time())<BR>lastdate=dateadd("yyyy",years,"2/12/2002 10:58:59 AM")<BR>months=datediff("m",lastdate,Date() & " " & Time())<BR>lastdate=dateadd("m",months,lastdate)<B R>days=datediff("d",lastdate,Date() & " " & Time())<BR>tseconds=datediff("s",lastdate,Date() & " " & Time())<BR>days=tseconds606024<BR>hours=(tseconds6 060)-(days*24)<BR>minutes=(tseconds60)-(hours*60)-(days*24*60)<BR>seconds=tseconds-(minutes*60)-(hours*60*60)-(days*24*60*60)<BR><BR><BR>for the year, month, and day intervals, i created a variable called "lastdate". in order to break down the exact number of each interval, i added the previous interval into the date from which the function was counting. however, after i got past the "day" interval, the numbers started getting wrong, i&#039;m assuming because of issues with rounding numbers. everything after that should be pretty self explanitory. yeah.

8. Senior Member
Join Date
Dec 1969
Posts
96,118

## That won't work...

Did you TRY it?<BR><BR>Try this little sample:<BR><BR>&#060;HTML&#062;&#060;BODY&#062;<BR >&#060;SCRIPT Language=VBScript&#062;<BR>dt1 = CDate("12/29/2001")<BR>dt2 = CDate("1/2/2002")<BR>document.write "date diff says " & datediff("yyyy",dt1,dt2) & " years&#060;P&#062;"<BR>document.write "date diff says " & datediff("m",dt1,dt2) & " months&#060;P&#062;"<BR>document.write "date diff says " & datediff("d",dt1,dt2) & " days"<BR>&#060;/SCRIPT&#062;<BR>&#060;/BODY&#062;&#060;/HTML&#062;<BR><BR>Only 4 days difference, yet datediff says they are both one year and one month apart!<BR><BR>Sorry, but I don&#039;t think you can use DateDiff in this way!<BR><BR>You&#039;ll just have to get the seconds and then convert them to the other units.<BR><BR>

#### Posting Permissions

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