vbscript date error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • user232k2038
    New Member
    • Sep 2007
    • 16

    vbscript date error

    I have a script that generates a calendar. It's worked fine for two or three years now, but at the beginning of this month, the days were incorrect. It displays February 1, 2008 as being a Wednesday, when it was a Friday.

    Here's the code:
    [CODE=vb]
    dim startday
    startday = weekday(month & "/" & day & "/" & year)
    select case startday
    case 1
    'Sunday
    case 2
    'Monday
    'etc.
    [/CODE]

    Instead of evaluating to 6, or Friday, weekday("2/1/2008") evaluates to 4, or Wednesday. However, weekday("1/31/2008") evaluates to 5, or Thursday. I checked the values for month, day, and year, and they are 2, 1, and 2008 respectively when the code fires.

    I even tried weekday("2/1/2008") on w3schools in the Try It editor, and it evaluated to 3 as well.

    Any ideas?
  • kadghar
    Recognized Expert Top Contributor
    • Apr 2007
    • 1302

    #2
    yes, it's because VB it's a "smart" dates reader, so if you write 12/31/07, since 31 cannot be the month, it assumes you're talking about December 31st.
    But if you write "2/1/2008" i'd say it wont use mm/dd/yyyy but dd/mm/yyyy and actually January the 2nd was wednesday.

    i'd recommend you to write a little function like:
    [CODE=vb]public function myMonth(byval mnth as integer) as string
    select case mnth
    case 1: myMonth = "Jan"
    case 2: myMonth = "Feb"
    case 3: myMonth = "Mar"
    '(...)
    end select
    end function[/CODE]

    and instead of:
    startday = Weekday(month & "/" & day & "/" & year)
    use:
    startday = Weekday(myMonth (month) & "/" & day & "/" & year)

    This might solve many date problems, but will create you a language issue. This code wont run in not-english versions.
    HTH

    Comment

    • user232k2038
      New Member
      • Sep 2007
      • 16

      #3
      That worked perfectly. Thanks! :D

      Comment

      • Killer42
        Recognized Expert Expert
        • Oct 2006
        • 8429

        #4
        To avoid the language issues, don't bother with the month name. Just use DateSerial() function, which accepts the year, month and day in a clearly defined sequence.

        For example...
        startday = Weekday(DateSer ial(Year, Month, Day))

        By the way, I recommend changing the names of your variables. Year, Month and Day are the names of functions in VB.

        Comment

        Working...