Date function

  • Thread starter Thread starter simon
  • Start date Start date
S

simon

I have the name of a day like for example 'Mon'
Is there some function which returns the number of a weekDay if you have the
day name or I should write the select case statements?

If the day is 'Mon' then result should be 2.

Thank you,
Simon
 
simon said:
I have the name of a day like for example 'Mon'
Is there some function which returns the number of a weekDay if you
have the day name or I should write the select case statements?

If the day is 'Mon' then result should be 2.


I thought the 7th day is Sunday...


I didn't find a built-in method. The following code should work. I assume
the weekday number is 1 to 7 and the first day of the week is Sunday.

Dim WeekdayNames As String() = { _
Nothing, "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" _
}

MsgBox(DirectCast(WeekdayNames, IList).IndexOf("Mon"))


If all you need to do with the array is getting the index, you can also
declare it as IList ...

Dim WeekdayNames As IList = New String() { _
Nothing, "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" _
}

.... to avoid type casting each time - or put this huge amount of code in a
procedure or a class.
 
Hi Simon,

I think I would do it like this.
\\\
Dim myfield As String = "Mon"
Dim mydays As String() = {"Sun", "Mon", "Tue", "rest"}
Dim i As Integer
For i = 0 To mydays.Length - 1
If mydays(i) = myfield Then
MessageBox.Show("I did find " & i.ToString & _
" while the day of week = " & New Date().DayOfWeek)
Exit For
End If
Next
Dim myday As Integer = i + 1
////
I hope this helps?

Cor
 
Hi Armin,

Nice one (array.indexof) and about that "nothing" I was thinking also, to
start the index on one.

:-)

Cor
 
Thank you for your answers.
I have another question:

If I have date="13.12.2003" and hour=9 and second =20 what is the best way
to get datetime value?

datetime=date+hour+second

Thank you,
Simon
 
Simon, there is a standard Enumeration called "DayOfWeek" which should be
used if possible. In this enumeration, Sunday=0 and Saturday=6. If you want
to use different values, then maybe you should create your own Enumeration
like:

Public Enum MyDaysOfTheWeek

Saturday = 0
Sunday = 1
Monday = 2

etc. etc.

End Enum

You could then use the [Enum].Parse method to turn the String "Monday" into
the value 2. E.g.
myDayOfWeekValue = CType([Enum].Parse(GetType(DaysOfWeek), "Monday"),
DayOfWeek)

I don't recommend using your own enumeration because it will be incompatible
with the DateTime.DayOfWeek() function (which returns what day of the week
the date variable is)
If I have date="13.12.2003" and hour=9 and second =20 what is the best way
to get datetime value?

To turn that date into a proper date time variable, use the following:

Dim myDate as New Date(2003, 12, 13, 9, 0, 20)

The date structure can take many different arguments as its constructor. In
the example above they are Year, Month, Day, Hour, Minute, Second.

Incedentally, the following will return Saturday (which is equal to 6)

Dim myDate as New Date(2003, 12, 13, 9, 0, 20)
debug.WriteLine(myDate.DayOfWeek)
debug.WriteLine(cint(myDate.DayOfWeek))

Hope this helps,

Trev.
 
Hi Simon,

I know no best ways, do you mean this?

Dim mydatetime As New DateTime(2003, 12, 13, 9, 1, 20)

I added the hour as one.

Cor
 
I thought the 7th day is Sunday...


I didn't find a built-in method. The following code should work. I assume

How about this:

Imports System.Globalization

Dim d As New DateTimeFormatInfo

MsgBox((Array.IndexOf(d.DayNames, "Monday") + 1).ToString)
MsgBox((Array.IndexOf(d.AbbreviatedDayNames, "Mon") + 1).ToString)
 
Hi Chris,

Even better for this problem I think.

But I found that filling of the table so nice that Armin was using and than
direct a indexof.

Cor
 
Chris Dunaway said:
MsgBox((Array.IndexOf(d.AbbreviatedDayNames, "Mon") + 1).ToString)

Aaaah! "AbbreviatedDayNames" - that's what I didn't find (when doing a
symbol search for "weekday"). :-)
 
simon said:
Thank you for your answers.
I have another question:

If I have date="13.12.2003" and hour=9 and second =20 what is the
best way to get datetime value?

datetime=date+hour+second

If you have no single variables to "connect" (like shown in the other
answers), you can also use date literals:

dim d as date
d = #12/13/2003 9:0:20#

That's what you type in. It gets converted to
d = #12/13/2003 9:00:20 AM#
 
Just remember to be careful of the American format dates though
(#mm/dd/yyyy# instead of #dd/mm/yyyy#).
 
Back
Top