I was wondering if there was a way to directly convert the integer DayOfWeek returns into a string representing the day like Monday, Tuesday etc.
Sample code:
MessageBox.Show(Date.Today.DayOfWeek)
This will return 6 (as of today). Is there a way to directly convert this into Saturday, for example? I don't really care what it really converts it into, but I want to do away with my Select Case:
Select Case Date.Today.DayOfWeek
Case 0
day = "Sunday"
Case 1
day = "Monday"
Case 2
day = "Tuesday"
Case 3
day = "Wednesday"
Case 4
day = "Thursday"
Case 5
day = "Friday"
Case 6
day = "Saturday"
Case Else
day = "Apocalypse: we're all boned."
End Select
Thanks :)
DateTimeFormatInfo.CurrentInfo.GetDayName.
A simpler way:
Dim day As String = Date.Today.DayOfWeek.ToString()
There's a DateTime format for that: dddd
Dim date1 As Date = #08/29/2008 7:27:15PM#
date1.ToString("dddd", CultureInfo.CreateSpecificCulture("en-US"))
With the CultureInfo you can get it in a specific language (it's optional)
For more info:
http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx#ddddSpecifier
DateTime.DayOfWeek doesn't return an integer - it returns an enum of type DayOfWeek. I'd expect that to be converted into the name automatically, but maybe this is a VB subtlety; maybe something to do with using Date instead of DateTime? Try this:
MessageBox.Show(DateTime.Today.DayOfWeek.ToString())
This won't be culture-sensitive though - it will always just display the name of enum value, in English. If that's not good for you, use Zyphrax or itowlson's solution.
Date.Today.DayOfWeek.ToString will give you what you're looking for. Nice and easy.
Just in case others looked at this example. I believe it should be Case 0 for Sunday.
Case 0
day = "Sunday"
Related
I've limited knowledge in coding with VBA in Excel and have run into a problem. I'm trying to write a code where it does something based on what day of the week it is.
I'm extracting the day of the week as string and then using a IF...ELSE bit. Code is attached below:
Sub Schedule()
Dim Today As String
Today = Format(Date, "dddd")
If Today >= "Monday" And Today <= "Thursday" Then
'code to do something
ElseIf Today = "Friday" Then
'code to do something
ElseIf Today >= "Saturday" And Today <= "Sunday" Then
'code to do something
End If
End Sub
Kindly advise.
A Select Case ... is fine for this - and do use constants, not magic numbers:
Sub Schedule()
Select Case Weekday(Date)
Case vbMonday To vbThursday
' Code to do something.
Case vbFriday
' Code to do something else.
Case vbSaturday, vbSunday
' Code to do some fun.
End Select
End Sub
Use WeekDay() to extract an integer representing the current day of the week:
Dim Today As VbDayOfWeek
Today = Weekday(Date)
If Today >= vbMonday And Today <= vbThursday Then
....
You cannot apply comparison operands (> or <) to strings so you will need to use the .WeekDay() function instead of formatting to string.
Here is the documentation: WeekDay please note the second optional parameter.
Here is the code:
Dim Today as Long
Today = WeekDay(Date)
If Today >= 2 And Today <= 5 Then
'do something
ElseIf Today = 6 Then
'do something
ElseIf Today = 7 Or Today = 1 Then
'do something
End If
DECLARE #Date DATE = '2/28/2014'
I need to test whether the DAY in the DATE above is the LAST day of the month and it should work for all months in the year.
If it's true, then I need to return a true, if not, then false.
E.g.
SET #Date = '2/27/2014'
This should return FALSE.
SET #Date = '12/31/2014'
This should return TRUE.
I know you can manipulate this based on month but I'm just wondering whether there is an easy way to do it.
An easy way that works in almost any version of SQL Server is:
select (case when month(#date) <> month(dateadd(day, 1, #date))
then 'true' else 'false'
end)
That is, add a day and see if you are in the same month. This works for leap years, and Dec 31 and so on.
This will return 1 if its the last day of the month, and 0 otherwise
DATEDIFF(month,#date,#date+1)
As no one has given the 2012+ answer as an answer yet...
SELECT IIF(EOMONTH(#date) = #date, 'Yes','No')
As an aside you should use an unambiguous format for date literals such as ISO yyyy-mm-dd to avoid surprises when the code is executed under a login with different default options.
I am attempting to grab the current day in the format 01 - 07 or 1 - 7.
This is the query I am using SELECT id FROM tbl_date WHERE weekday = WEEKDAY(NOW) but I seem to get false in return so am not sure I am using the function correctly. The table column weekday contains a number 1 - 7 where 1 is sunday/monday.
Any ideas?
Stupid misstake by me. The correct way would be weekday = WEEKDAY(NOW()) since NOW() itself is a function.
MSSQL can use DATEPART(dw, GETDATE()), MySQL can use DAYOFWEEK()
I have four different DateTime boxes. Two boxes just displays the Date and the other two just Displays the time
If the current time is between midnight and six am I want the Date in the date box to be the day before.
I have it all, I'm just missing the part that compares the two.
Dim currentTime As DateTime = Now
'default date
If deMaxDate.Value = Nothing Then
deMaxDate.Value = Now
End If
If deMinDate.Value = Nothing Then
If currentTime.Hour < TimeOfDay.Hour Then
'THIS IF STATMENT IS WRONG - HOW CAN I CHECK IF ITS BETWEEN 12AM AND 6 HERE
deMinDate.Value = (Now - TimeSpan.FromDays(1))
Else
deMinDate.Value = Now
End If
End If
'default time
If teMaxTime.Value = Nothing Then
teMaxTime.Value = Now
End If
If teMinTime.Value = Nothing Then
teMinTime.Value = (Now - TimeSpan.FromHours(6))
End If
My comment by the third if statment is where I'm stuck at.
DateTime is a double datatype? Something like
if currentTime.Hour < TimeOfDay.Hour.Equals(6)
?
Your rule, if I understand this, is that you want to look at the current time. If the current time is between 12AM(0000) and 6AM(0600), then you want to use yesterday as the active date.
Dim current as DateTime = now()
Dim activeDate as DateTime = current
if current.Hour < 6 then
activeDate = current.AddDays(-1)
end if
Although, if you're not really interested in the hours part of the date except for this business rule, you could always just do date.AddHours(-6).
I do this in a similar situation where I want to check the current time is prior to 4 AM.
If (DateTime.Now.Hour < 4) Then
'do something
End If
Just use
If currentTime.Hour <= 6 Then
deMinDate.Value = Now.AddDays(-1)
Else
deMinDate.Value = Now
End If
or indeed...
If currentTime.Hour <= 6 Then
deMinDate.Value = Now.Date.AddDays(-1)
Else
deMinDate.Value = Now.Date
End If
if you don't want the time bit because .Now contains a time element as well as a date element.
Have a look at http://msdn.microsoft.com/en-us/library/5ata5aya.aspx. It might apply to your case
currentTime.Hour < TimeOfDay.Hour.Equals(6)
ended up being the answer
Ok, so I need to find the date of Monday this week programmatically.
For example, for this week Monday was on the 9th, so the date I need is: 09/11/2009
And when we roll over to next week it needs to calculate: 16/11/2009
I have tried doing this myself but I can't see how to do the arithmetic, thank you.
C#:
date.AddDays(1 - (date.DayOfWeek == DayOfWeek.Sunday ? 7 : (int)date.DayOfWeek));
VB.NET:
date.AddDays(1 - IIf((date.DayOfWeek = DayOfWeek.Sunday), 7, date.DayOfWeek))
Dim thisMonday As Date = Now.AddDays((Now.DayOfWeek - 1) * -1).Date
If today is a Sunday it gives the following Monday otherwise, gives the Monday this week.
Return givenDate.AddDays(1 - CType(IIf((givenDate.DayOfWeek = DayOfWeek.Sunday), 7, givenDate.DayOfWeek), Double))
If givenDate is a Sunday, counts back to the preceding Monday. Includes a CType to cast the IIf result to a Double to work with Option Strict On.