Round up date to 1st or 15th of month

# Thread: Round up date to 1st or 15th of month

1. NNick Guest

## Round up date to 1st or 15th of month

Can anyone help!<BR>I need to know what function t use to round up to either the 1st of the month or 15th of the month. <BR><BR>&#060;%@ LANGUAGE="VBScript" %&#062;<BR>&#060;%<BR>Response.write Dateadd("d",45, now)<BR>%&#062;<BR><BR>Thanks

2. Junior Member
Join Date
Dec 1969
Posts
8

## RE: Round up date to 1st or 15th of month

BAH: messed this up and misposted it earlier anyway.. try this.. dont know if the date functions work in ASP.. if they dont, you could always just make this the body in a DLL ;) LAF<BR><BR>Function RoundDate(ParamMyDate As Date) As Date<BR><BR>Dim MyTargetDate As Date<BR>Dim MyNewDate As Date<BR>Dim MyDateDiff As Integer<BR>MyTargetDate = CDate("01/15/2001")<BR>MyDateDiff = DateDiff("d", MyTargetDate, ParamMyDate)<BR><BR>If MyDateDiff &#060; 0 Then<BR> MyNewDate = ParamMyDate + Abs(MyDateDiff)<BR> RoundDate = MyNewDate<BR>End If<BR>If MyDateDiff &#062; 0 Then<BR> MyNewDate = DateAdd("m", 1, ParamMyDate)<BR> RoundDate = MyNewDate<BR>End If<BR>If MyDateDiff = 0 Then<BR> RoundDate = ParamMyDate<BR>End If<BR>If DateDiff = -15 Then<BR> RoundDate = ParamMyDate<BR>End If<BR><BR>End Function<BR>

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

## I don't get that code...

...how do you *know* what the target date is???<BR><BR>I think it&#039;s a lot simpler than all that:<BR><BR>&#060;%<BR>theDate = ... &#039; this is the date to be rounded...<BR><BR>theDay = Day( theDate ) &#039; what day of the month?<BR>If theDay &#060;&#062; 1 AND theDay &#060;&#062; 15 Then<BR>&nbsp; &nbsp; &#039; must adjust...<BR>&nbsp; &nbsp; If theDay &#062; 15 Then<BR>&nbsp; &nbsp; &nbsp; &nbsp; &#039; round to first day of next month<BR>&nbsp; &nbsp; &nbsp; &nbsp; theDate = DateSerial( Year(theDate), Month(theDate)+1, 1 )<BR>&nbsp; &nbsp; Else <BR>&nbsp; &nbsp; &nbsp; &nbsp; &#039; round to 15th of this month<BR>&nbsp; &nbsp; &nbsp; &nbsp; theDate = DateSerial( Year(theDate), Month(theDate), 15 )<BR>&nbsp; &nbsp; End If<BR>End If<BR>%&#062;<BR><BR>Now theDate will be the 1st of the given month (only if that&#039;s what it originally was), the 15th of the given month (if it was the 2nd to 15th originally), or the 1st of the next month after the given month (if it was any day after the 15th).<BR><BR>If you want to round the 1st of the given month up to the 15th of this month, then the code is even simpler!<BR><BR>&#060;%<BR>theDate = ... &#039; this is the date to be rounded...<BR><BR>If Day( theDate ) &#062; 15 Then<BR>&nbsp; &nbsp; &#039; round 16 to 31 to first day of next month<BR>&nbsp; &nbsp; theDate = DateSerial( Year(theDate), Month(theDate)+1, 1 )<BR>Else <BR>&nbsp; &nbsp; &#039; round 1 to 15 to 15th of this month<BR>&nbsp; &nbsp; theDate = DateSerial( Year(theDate), Month(theDate), 15 )<BR>End If<BR>%&#062;<BR>

4. NNick Guest

## RE: I don't get that code...

Thank you both very much

#### Posting Permissions

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