469,647 Members | 1,796 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,647 developers. It's quick & easy.

Date function

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

Nov 20 '05 #1
11 1403
Cor
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
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.

Nov 20 '05 #2
"simon" <si*********@stud-moderna.si> schrieb
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.
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #3
Cor
Hi Armin,

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

:-)

Cor
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" _
}

Nov 20 '05 #4
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


"Cor" <no*@non.com> wrote in message
news:uf**************@TK2MSFTNGP11.phx.gbl...
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
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.


Nov 20 '05 #5
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.
"simon" <si*********@stud-moderna.si> wrote in message
news:uT**************@TK2MSFTNGP10.phx.gbl... 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


"Cor" <no*@non.com> wrote in message
news:uf**************@TK2MSFTNGP11.phx.gbl...
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
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.



Nov 20 '05 #6
Cor
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

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

Nov 20 '05 #7
On Mon, 8 Dec 2003 11:55:56 +0100, Armin Zingler wrote:
"simon" <si*********@stud-moderna.si> schrieb
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


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)

--
Chris

To send me an E-mail, remove the underscores and lunchmeat from my E-Mail
address.
Nov 20 '05 #8
Cor
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

Imports System.Globalization

Dim d As New DateTimeFormatInfo

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

Nov 20 '05 #9
"Chris Dunaway" <dunawayc@_lunchmeat_sbcglobal.net> schrieb
MsgBox((Array.IndexOf(d.AbbreviatedDayNames, "Mon") + 1).ToString)


Aaaah! "AbbreviatedDayNames" - that's what I didn't find (when doing a
symbol search for "weekday"). :-)
--
Armin

Nov 20 '05 #10
"simon" <si*********@stud-moderna.si> schrieb
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#
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #11
Just remember to be careful of the American format dates though
(#mm/dd/yyyy# instead of #dd/mm/yyyy#).

"Armin Zingler" <az*******@freenet.de> wrote in message
news:uR****************@TK2MSFTNGP11.phx.gbl...
"simon" <si*********@stud-moderna.si> schrieb
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#
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by androtech | last post: by
30 posts views Thread by Dr John Stockton | last post: by
1 post views Thread by Liz Malcolm | last post: by
10 posts views Thread by John Morgan | last post: by
12 posts views Thread by Assimalyst | last post: by
3 posts views Thread by dave | last post: by
10 posts views Thread by Jes | last post: by
3 posts views Thread by murch.alexander | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.