How to get the 'Name' of a day

Hi,

I have the following string:
“2014-01-10”

So, it’s a date

But, how can I get the weekdays name like “Friday” ?

I hate working with dates :smiley:

I found this VB snippet. Should not be too difficult to translate into Xojo :

[code]Public Function DOW(ByVal GregDate As Date) As String
’ Return values:
’ 0 = Sunday
’ 1 = Monday
’ 2 = Tuesday
’ 3 = Wednesday
’ 4 = Thursday
’ 5 = Friday
’ 6 = Saturday
Dim y As Integer
Dim m As Integer
Dim d As Integer

' monthdays:
' This is a "template" for a year. Each number
' stands for a day of the week. The general idea
' is that, in a standard year, if Jan 1 is on a
' Friday, then Feb 1 will be a Monday, Mar 1
' will be a Monday, April 1 will be a Thursday,
' May 1 will be Saturday, etc..
Dim mcode As String
Dim monthdays() As String
monthdays = Split("5 1 1 4 6 2 4 0 3 5 1 3")

' Grab our date info
y = Val(Format(GregDate, "yyyy"))
m = Val(Format(GregDate, "mm"))
d = Val(Format(GregDate, "dd"))

' Snatch the corresponding month code
mcode = Val(monthdays(m - 1))

' Multiplying by 1.25 takes care of leap years,
' but not completely. Jan and Feb of a leap year
' will end up a day extra.
' The 'mod 7' gives us our day.
DOW = ((Int(y * 1.25) + mcode + d) Mod 7)

' This takes care of leap year Jan and Feb days.
If y Mod 4 = 0 And m < 3 Then DOW = (DOW + 6) Mod 7

End Function[/code]

I’d use Date.LongDate and try to extract the day name from it.

Better :

dim days(7) as string days(1) = "Sunday" days(2) = "Monday" days(3) = "Tuesday" days(4) = "Wednesday" days(5) = "Thursday" days(6) = "Friday" days(7) = "Saturday" dim d as new date label1.text=days(d.dayofweek)

And the day names are not system dependent. So you can have days of the week in several languages on the same machine.

This is a function which returns the name of the week day on Cocoa considering the user preferences in the “Language & Text” pane:

Function WeekDayName(weekDayIndex As Integer) As String Declare Function NSClassFromString Lib "Foundation" (ClassName As CFStringRef) As Ptr Declare Function alloc Lib "Foundation" Selector "alloc" (NSClass As Ptr) As Ptr Declare Function init Lib "Foundation" Selector "init" (NSObject As Ptr) As Ptr Declare Function objectAtIndex Lib "Foundation" Selector "objectAtIndex:" (NSArray As Ptr, index As UInt32) As CFStringRef Declare Function currentLocale Lib "Foundation" Selector "currentLocale" (NSLocale A s Ptr) As Ptr Declare Sub setLocale Lib "Foundation" Selector "setLocale:" (NSDateFormatter As Ptr , NSLocale As Ptr) Declare Function weekdaySymbols Lib "Foundation" Selector "weekdaySymbols" (NSDateFormatter As Ptr) As Ptr Dim dateFormatter As Ptr = init(alloc(NSClassFromString("NSDateFormatter"))) Dim currentLocale As Ptr = currentLocale(NSClassFromString("NSLocale")) setLocale(dateFormatter, currentLocale) Dim weekdaySymbolArray As Ptr = weekdaySymbols(dateFormatter) If weekDayIndex >= 0 Or weekDayIndex <= 6 Then Return objectAtIndex(weekdaySymbolArray, weekDayIndex) Else Raise New OutOfBoundsException() End End Function
Example usage:

Dim arr() As String For i As Integer = 0 To 6 arr.Append(WeekDayName(i)) Next MsgBox(Join(arr, EndOfLine))

Well! Thanks! But I got another and easier solution: I split the longname after the Space and take the first string in the array!

sometimes it’s to simple :slight_smile:

[quote]dim thisdate as new date
thisdate.year = something
thisdate.month = something
thisdate.day = something

dim a1() as string = split(ThisDate.longname, " ")
dim a2 as string = a1(0)[/quote]

You could use NthField: dim a1() as string = ThisDate.longname.NthField(", ", 1)

But be careful, there might be date formats of other languages than German and English where the order of the parts is different.

It works fine, but you are tied to the system settings, in particular to the language. So your software maybe in one language, and because the system is set otherwise, you may end up with a week date in another. Strange :S

[quote=67251:@Lars Lehmann]Hi,

I have the following string:
“2014-01-10”

So, it’s a date

But, how can I get the weekdays name like “Friday” ?[/quote]
http://www.great-white-software.com/REALbasic_Code.html
Look for “A project with two methods for figuring out (in pure RB) the local day and month abbreviated names”
It probably needs to be updated

how about

dow=NthField("Sunday|Monday|Tuesday|Wednesday|Thursday|Friday|Saturday","|",dt,dayofweek)

not tied to any specific format or locale

Thats precisely the problem if you want the days in french, german, swedish etc :slight_smile:
They’re correct for a tiny fragment of the worlds population (about 8% according to
http://msdn.microsoft.com/en-us/library/windows/desktop/ee264325(v=vs.85).aspx)

And how is that different from dealing with any other string in you app?

make the string of names a system const, and define as many languages as you care to support

If handling the declares is too much, Lars, why not go for something easier?
Take a reference date which you know its weekday, calculate the days since that and make a modulo division by 7. For example:

[code] dim mydate as new date
dim reference as new date
reference.year=1900
reference.month=1
reference.day=1

dim difference as integer = (mydate.TotalSeconds-reference.TotalSeconds) / 86400 mod 7 // 86400 = seconds in a day[/code]

Jan 01 1900 was a Monday, and the function returns the difference to a Monday this way. Which means you can pick the result and replace it by localized strings the way you prefer: 1 = Tuesday … …

[quote=67534:@Dave S]And how is that different from dealing with any other string in you app?
make the string of names a system const, and define as many languages as you care to support[/quote]
Get them dynamically and save yourself the trouble

FormatDate(D As Date, xF as String)

[code]
Dim sDate as String = Lowercase(xF)

Dim DaysLong() As String = Array("", “Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday”, “Sunday”)
Dim DaysShort() As String = Array("", “Mon”, “Tue”, “Wed”, “Thu”, “Fri”, “Sat”, “Sun”)
Dim MonthsLong() As String = Array("", “January”, “February”, “March”, “April”, “May”, “June”, “July”, “August”, “September”, “October”, “November”, “December”)
Dim MonthsShort() As String = Array("", “Jan”, “Feb”, “Mar”, “Apr”, “May”, “Jun”, “Jul”, “Aug”, “Sep”, “Oct”, “Nov”, “Dec”)

sDate = sDate.ReplaceAll(“yyyy”, D.Year.ToText)
sDate = sDate.ReplaceAll(“yy”, CStr(D.Year mod 100))

sDate = sDate.ReplaceAll(“hh”, Right(“0”+ CStr(d.Hour),2))
sDate = sDate.ReplaceAll(“h”, CStr(d.Hour))

sDate = sDate.ReplaceAll(“nn”, Right(“0”+ CStr(d.Minute), 2))
sDate = sDate.ReplaceAll(“n”, CStr(d.Minute))

sDate = sDate.ReplaceAll(“ss”, Right(“0”+ CStr(d.Second), 2))
sDate = sDate.ReplaceAll(“s”, CStr(d.Second))

sDate = sDate.ReplaceAll(“dddd”, DaysLong(D.DayOfWeek))
sDate = sDate.ReplaceAll(“ddd”, DaysShort(D.DayOfWeek))
sDate = sDate.ReplaceAll(“dd”, Format(D.Day, “00”))

sDate = sDate.ReplaceAll(“mmmm”, MonthsLong(D.Month))
sDate = sDate.ReplaceAll(“mmm”, MonthsShort(D.Month))
sDate = sDate.ReplaceAll(“mm”, Format(D.Month, “00”))

Return sDate[/code]