Looping through a number - Posted (1295 Views)
Starting Member
Fromafarr
Posts: 7
7
Hi,
I am trying to write a script that will take a datediff integer and divide it into days weeks, months. I need it to give me the weeks and or days left over.
I am trying to achieve this looping through the number but with no luck. In this case a month has to be 30 days which i think should make it easier than dealing with calendar months.
Thanks in advance.
 Sort direction, for dates DESC means newest first  
 Page size 
Posted
Forum Moderator
AnonJr
Posts: 5768
5768
Have you tried asking over at http://stackoverflow.com ? While we do answer the occasional general programming question, we tend to do better with forum related questions... this being the forum's support forum and all.
Posted
Advanced Member
Carefree
Posts: 4224
4224
That's basic math. If your datediff is 2497, for example:

Code:

<%
intVal=2497
' Obtain the months
intMM=int(intVal/30)
Response.Write intMM&"=Months<br>"
' Obtain remainder
intMML=intVal-(30*intMM)
' Calculate weeks (if any)
if intMML/7 >= 1 then
intWW=int(intMML/7)
Response.Write intWW&"=Weeks<br>"
intDD=intMML-(7*intWW)
Response.Write intDD&"=Days"
else
' Calculate days (if any)
intDD=intMML
Response.Write intDD&"=Days"
end if
%>

This results in the following output:

83=Months
1=Weeks
0=Days

Now all you do is populate the intVal with a variable and you're all set.
Posted
Support Moderator
Doug G
Posts: 6493
6493
I'd just use the datepart function.
http://msdn.microsoft.com/en-us/library/4kt42529(VS.85).aspx


======
Doug G
======
Computer history and help at www.dougscode.com
Posted
Advanced Member
Carefree
Posts: 4224
4224
The datepart isn't going to use a standard 30 day month.
Here's a version where you can fill in a blank and get your results. Save as "datediff.asp".
Code:

<%
Response.Write "<form action=""DateDiff.asp"" method=""post"" name=""DateDiff"">" & vbNewLine & _
"<table width=""30%"" align=""center"" bgcolor=""cyan"" borders=""0"" cellpadding=""1"" cellspacing=""3"">" & vbNewLine & _
" <tr valign=""middle"">" & vbNewLine & _
" <td align=""center"" bgcolor=""lightcyan"" colspan=""6"">Date Difference" & vbNewLine & _
" </td>" & vbNewLine & _
" </tr>" & vbNewLine & _
" <tr valign=""middle"">" & vbNewLine & _
" <td align=""center"" bgcolor=""lightgrey"" colspan=""6"">" & vbNewLine & _
" <input style=""text-align: center; text-decoration:blink;"" type=""text"" name=""Val"" size=""5"" maxlength=""5"" value=""" & Request.Form("Val") & """>" & vbNewLine & _
" </td>" & vbNewLine & _
" </tr>" & vbNewLine & _
" <tr valign=""middle"">" & vbNewLine & _
" <td align=""center"" bgcolor=""lightcyan"" width=""(100/6)%"">Months" & vbNewLine & _
" </td>" & vbNewLine
intVal=Request.Form("Val")
intMM=int(intVal/30)
strMM=cStr(intMM)
intMML=intVal-(30*intMM)
if intMML/7 >= 1 then
intWW=int(intMML/7)
strWW=cStr(intWW)
intDD=intMML-(7*intWW)
strDD=cStr(intDD)
if intDD=0 then strDD="0"
else
strWW="0"
intDD=intMML
strDD=cStr(intDD)
if strDD="" then strDD="0"
end if
Response.Write " <td align=""center"" bgcolor=""white"" width=""(100/6)%"">" & strMM & "" & vbNewLine & _
" </td>" & vbNewLine & _
" <td align=""center"" bgcolor=""lightcyan"" width=""(100/6)%"">Weeks" & vbNewLine & _
" </td>" & vbNewLine & _
" <td align=""center"" bgcolor=""white"" width=""(100/6)%"">"& strWW & "" & vbNewLine & _
" </td>" & vbNewLine & _
" <td align=""center"" bgcolor=""lightcyan"" width=""(100/6)%"">Days" & vbNewLine & _
" </td>" & vbNewLine & _
" <td align=""center"" bgcolor=""white"" width=""(100/6)%"">" & strDD & "" & vbNewLine & _
" </td>" & vbNewLine & _
" </tr>" & vbNewLine & _
"</table>" & vbNewLine & _
"<center><input type=""submit"" value=""Submit"" id=""submit""></center>" & vbNewLine & _
"</form>" & vbNewLine
%>
Posted
Starting Member
Fromafarr
Posts: 7
7
Thanks so much.
Posted
Support Moderator
Doug G
Posts: 6493
6493
The datepart isn't going to use a standard 30 day month
Maybe because 30 days isn't standard for a month? And the OP's premise that using 30 day months would be easier isn't correct, you have to do a lot more coding to deal with a non-calendar month interval since all the hard work about dealing with dates has already been done for you when you use the built-in date functions.
======
Doug G
======
Computer history and help at www.dougscode.com
 
You Must enter a message