Brainteaser: Convert Day of Week to Number

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
72,244
Office Version
  1. 365
Platform
  1. Windows
We have a situation where we have a table in a database that has a field representing "days of the week". It is formatted as Text (String) and is actually the day spelled out (i.e. "Monday").

I have to use this in some date calculations, so I need to convert this to a numerical value, i.e.
"Sunday" = 1
"Monday" = 2
...
"Saturday" = 7

It seems like it should be pretty easy to do, but I haven't found any functions that seem to do it (not many go from Text to Date formats).

So I created a UDF that does this, and does a fine job:
Code:
Function DayOfWeekValue(myDay As Variant) As Integer
'   Convert text value of day of week (i.e. "Monday") to numeric representation
 
    Select Case myDay
        Case "Sunday"
            DayOfWeekValue = 1
        Case "Monday"
            DayOfWeekValue = 2
        Case "Tuesday"
            DayOfWeekValue = 3
        Case "Wednesday"
            DayOfWeekValue = 4
        Case "Thursday"
            DayOfWeekValue = 5
        Case "Friday"
            DayOfWeekValue = 6
        Case "Saturday"
            DayOfWeekValue = 7
        Case Else
            DayOfWeekValue = 0
    End Select
            
End Function
The thing that bugs me is I can't help shake the feeling that there must be some cool formulaic way of doing this (that isn't too complex) that doesn't require a UDF, and I am just missing it.

Am I missing something easy?
Or does it require a UDF or a complex formula?

Really, this question is just to satisfy my own curiosity (as I said, I have a working solution)...
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
wonder how the performance would be you made a new table containing 2 fields and 7 rows --
DayOfWeek : NumberOfDay
Sunday 1
Monday 2
...
Saturday 7

and then in your current table, put an index on the DayofWeek field and then linked the 2 tables
 
Upvote 0
Here's a shorter version which was provided by Bob Askew on the Access World Forums website:

Rich (BB code):
'*******************************************<?XML:NAMESPACE PREFIX = O /><O:P></O:P>
Public Function RtnMonthNum(pstrMonName As String) As Integer
'*******************************************
'Purpose:   Returns month number (1 - 12)
'           when provided a complete or partial
'           month name.
'Coded by:  raskew
'Input:     From debug (immediate) window:
'           1) ? RtnMonthNum("April")
'           2) ? RtnMonthNum("Sep")
'Output: 1) 4
'           2) 9
'*******************************************<O:P></O:P>
 <O:P></O:P>
Dim strHold  As String
Dim strMonth As String
Dim intMonth As Integer<O:P></O:P>
 <O:P></O:P>
   strMonth = "JanFebMarAprMayJunJulAugSepOctNovDec"
   strHold = Left(pstrMonName, 3)
   intMonth = InStr(strMonth, strHold)
   RtnMonthNum = intMonth \ 3 + 1<O:P></O:P>
 <O:P></O:P>
End Function
 
Upvote 0
Thanks James, thought of that too.

Bob, that was for months, not days, but I see one could apply the same logic.

But no formula solutions, eh?
Guess I didn't overlook anything obvious, which makes me feel better (I guess!).
 
Upvote 0
Oops, yeah, you could use the same type of setup. And no, there is no formula solution, although I wish they would add one.
 
Upvote 0
No suggestion for anything in Access, but a different UDF:

Code:
Function DayOfWeekValue(myDay As Variant) As Integer
    Const sDays As String = "Sunday    Monday    Tuesday   Wednesday Thursday  Friday    Saturday"
        DayOfWeekValue = (InStr(1, sDays, myDay, vbTextCompare) + 9) \ 10
End Function
 
Upvote 0
And here a little bit of all the above suggestions put together :biggrin:

Code:
Public Function DayOfWeek(ByVal vDay As Variant) As Integer
Const s As String = "SuMoTuWeThFrSa"
DayOfWeek = (InStr(1, s, Left(vDay, 2), vbTextCompare) + 1) / 2
End Function
 
Last edited:
Upvote 0
At a (very) quick look around, you could use datepart("w",<date>) to get the number. It would need to be used ini a query though.
 
Upvote 0
Does the code in post #6 (or #7) actually need to be in a UDF?

Can't you use an expression like this?

DayNum: (InStr(1,"Sunday Monday Tuesday Wednesday Thursday Friday Saturday",[FieldName],1)+9)\10
 
Upvote 0
A function that probably doesn't look good:
=MATCH(A1,{"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"},0)
 
Upvote 0

Forum statistics

Threads
1,214,923
Messages
6,122,283
Members
449,075
Latest member
staticfluids

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top