Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date Range Needed.

Status
Not open for further replies.

Nebooaw

Programmer
Jun 1, 2001
142
GB
Hi, i am trying to sort out some code to return me a date range for any given month from the start of the month to the end of the month (also for a quarter ie. Jan - Mar, etc...) Please can anyone help???

The code below is what i use to determin the start and end of a week.

request.form("Period") is 1 to 52


YearStart = DateSerial(Year(Date()), 1, 1)
WeekStart = (YearStart - DatePart("w", YearStart)) + 1
WeekEnd = WeekStart + 6
Week_Number = request.form("Period")
WeekStart_Var = FormatDateTime(WeekStart + (7 * (Week_Number - 1)) ,vbShortDate)
WeekEnd_Var = FormatDateTime(WeekEnd + (7 * (Week_Number - 1)) ,vbShortDate)
Week_Start_dd_var = Cstr(Day(WeekStart_Var))
Week_Start_mm_var = Cstr(Month(WeekStart_Var))
Week_Start_yyyy_var = Cstr(Year(WeekStart_Var))
Week_End_dd_var = Cstr(Day(WeekEnd_Var))
Week_End_mm_var = Cstr(Month(WeekEnd_Var))
Week_End_yyyy_var = Cstr(Year(WeekEnd_Var))
If Week_Start_dd_var < 10 Then Week_Start_dd_var = &quot;0&quot; + Week_Start_dd_var
If Week_Start_mm_var < 10 Then Week_Start_mm_var = &quot;0&quot; + Week_Start_mm_var
If Week_End_dd_var < 10 Then Week_End_dd_var = &quot;0&quot; + Week_End_dd_var
If Week_End_mm_var < 10 Then Week_End_mm_var = &quot;0&quot; + Week_End_mm_var
DateRange = Week_Start_dd_var + &quot;/&quot; + Week_Start_mm_var + &quot;/&quot; + Week_Start_yyyy_var + &quot; 00:00:00&quot; + &quot; - &quot; + Week_End_dd_var + &quot;/&quot; + Week_End_mm_var + &quot;/&quot; + Week_End_yyyy_var + &quot; 23:59:59&quot;


Kind Regards.
 
To get the whole month given any date...

anyDate = cDate(&quot;3/15/2003&quot;)
monthStart = cDate(datePart(&quot;m&quot;,anyDate) & &quot;/1/&quot; & datePart(&quot;yyyy&quot;, anyDate))
monthEnd = dateAdd(&quot;m&quot;,1,monthStart) - 1

I'm not sure how you need to use the quarter

anyDate = cDate(&quot;3/15/2003&quot;)
quartNum = datePart(&quot;q&quot;,anyDate)
select case quartNum
case 1
quartStart = cDate(&quot;1/1/&quot; & datePart(&quot;yyyy&quot;,anyDate)
quartEnd = cDate(&quot;3/31/&quot; & datePart(&quot;yyyy&quot;,anyDate)
case 2
quartStart = cDate(&quot;4/1/&quot; & datePart(&quot;yyyy&quot;,anyDate)
quartEnd = cDate(&quot;6/30/&quot; & datePart(&quot;yyyy&quot;,anyDate)
case 3
quartStart = cDate(&quot;7/1/&quot; & datePart(&quot;yyyy&quot;,anyDate)
quartEnd = cDate(&quot;9/30/&quot; & datePart(&quot;yyyy&quot;,anyDate)
case 4
quartStart = cDate(&quot;10/1/&quot; & datePart(&quot;yyyy&quot;,anyDate)
quartEnd = cDate(&quot;12/31/&quot; & datePart(&quot;yyyy&quot;,anyDate)
end select

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rich Cook
 
Thanks, i need though to format it the same way as the code fot the week. I need to provide a month number.

Regards.
 
I'm still not clear as to what you need. You have the starting and ending dates of the month and quarter. Please explain what else you need.

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rich Cook
 
I have sorted it out now, just got a bit mixed up. Works a treat!

Thanks for you help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top