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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Comparing Dates with VBSCRIPT

Status
Not open for further replies.

jasonhuibers

Programmer
Sep 12, 2005
290
CA
Using vbscript I want to use this condition:

lYear = Year(Date())

if date() >= "01/01" & "/" & lYear and date() < "04/01/" & "/" & lYear then
var2 = "01/01" & "/" & lYear
elseif date() >= "04/01" & "/" & lYear and date() < "07/01/" & "/" & lYear then
var2 = "04/01" & "/" & lYear
elseif date() >= "07/01" & "/" & lYear and date() < "10/01/" & "/" & lYear then
var2 = "07/01" & "/" & lYear
elseif date() >= "10/01" & "/" & lYear and date() <= "12/31/" & "/" & lYear then
var2 = "10/01" & "/" & lYear
end if
var2 = cdate(var2)


However todays date does not pass through any of these conditions... var2 should return 10/01/2008 because todays date is between 10/01/2008 and 12/31/2008
 
Well, for one thing you are putting an extra / in your dates:

if date() >= "01/01" & "/" & lYear and date() < "04/01[red]/" & "/"[/red] & lYear then

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Maybe something like:
Code:
Option Explicit

Function QuarterStart(ByVal DateVal)
  QuarterStart = _
    DateSerial(Year(DateVal), _
               3 * (DatePart("q", DateVal) - 1) + 1, _
               1)
End Function

Msgbox CStr(QuarterStart(#1-Jan-2000#))
Msgbox CStr(QuarterStart(#31-Mar-2008#))
Msgbox CStr(QuarterStart(#30-Sep-1999#))
Msgbox CStr(QuarterStart(#31-Dec-1950#))
This also doesn't have to deal with ambiguities in string date formats across locales that can occur with a delimited number format.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top