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

Convert "Thursday" to dayofweek (6) 1

Status
Not open for further replies.

Glowworm27

Programmer
May 30, 2003
587
US
Howdy all,

I have a program that should download a file every thursday at 4:00 pm.

I was trying to convert the string "Thursday" to the DayofWeek Integer value of 6, but I get an invalid cast exception if I do something like this

idayofweek = Ctype("Thursday", DayofWeek)

can someone show me how to convert the String to the dayofweek function ? or something?

thanks
[cannon]

George Oakes
Check out this awsome .Net Resource!
 
not sure if this is what you need but you can convert a date to day of week integer using...

dim dt as date=now
dim int as integer=a.dayofweek

you can also use the enumeration to help

dayofweek.monday
dayofweek.tuesday...
 
Code:
[Enum].Parse(GetType(DayOfWeek), "Thursday")
Thursday returns 4 because sunday is 0.
You'll have to write:
Code:
[Enum].Parse(GetType(DayOfWeek), "Thursday") + 2 Mod 7
But then Saturday is 8...
You'll have to figure it out.
 
korach,

that was close but it did not return an integer on the word Thursday

So I just wrote a function

Code:
Public Function GetWeekDay(ByVal strWeekDay) As Integer
        Select Case strWeekDay
            Case "Sunday"
                Return 1
            Case "Monday"
                Return 2
            Case "Tuesday"
                Return 3
            Case "Wednesday"
                Return 4
            Case "Thursday"
                Return 5
            Case "Friday"
                Return 6
            Case "Saturday"
                Return 7
            Case Else
                Return -1
        End Select
    End Function

George Oakes
Check out this awsome .Net Resource!
 
Code:
Dim i As Integer = [Enum].Parse(GetType(DayOfWeek), "Thursday")
returned integer to me. If you use option strict:
Code:
Dim i As Integer = CInt([Enum].Parse(GetType(DayOfWeek), "Thursday"))
 
korach,

Ok that helped out, but for some reason it returned a 4 and not a 5,

I am using XP Pro, standard US settings.

this is the line I am trying to evaluate
Code:
If DatePart(DateInterval.Weekday, dCurrentDateTime) = CInt([Enum].Parse(GetType(DayOfWeek), sDownloadDay)) Then

dCurrentDateTime = 1/27/2005 10:00 am
, sDownloadDay = "Thursday"

This line should evaluate to True, but it is not because the ([Enum].Parse(GetType(DayOfWeek), sDownloadDay)) is returning a 5, I didnt think days of the week were 0 based? I thought Sunday (first day of the week) was a 1? I could be wrong , I will check it out, but Thanks for your help. Star for You !

G

George Oakes
Check out this awsome .Net Resource!
 
wow, Day of the week is zero based 0 = sunday, 6= saturday

from MDSN...

The DayOfWeek enumeration represents the day of the week in calendars that have seven days per week. This enumeration ranges from zero, indicating Sunday, to six, indicating Saturday.

George Oakes
Check out this awsome .Net Resource!
 
Thanks for the star. You could save the msdn search. I wrote in my first answer that sunday is 0.
I didn't know you have a date var. If so, you can compact your code to:
Code:
If dCurrentDateTime.DayOfWeek.ToString() = sDownloadDay Then
I assumed that dCurrentDateTime is a date and sDownloadDay is the string of the day. The function ToString() for enums returns the string equivalent to the value. CStr(EnumValue) returns the integer value as a string.

P.S.
Your code will work until Microsoft will decide to change the DayOfWeek day names... Listen to the news, you might be surprised!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top