How do I get the day of the week as a string?
weekday () method to get the day of the week as a number, where Monday is 0 and Sunday is 6. isoweekday () method to get the weekday of a given date as an integer, where Monday is 1 and Sunday is 7. strftime () method to get the name of the day from the date. Like Monday, Tuesday.
WeekDay = WEEKDAY ('calendar' [Date]) The default setting is that the weekday starts on Sunday being 1, and then Saturday is 7. You can change it with the 2nd parameter of the Weekday function as below: Return type: 1, week begins on Sunday (1) and ends on Saturday (7). numbered 1 through 7.
In JavaScript, we can get the day of the week using the getDay() method. This will return a number from 0-6, with 0 being Sunday, and 6 being Saturday. Skip to primary navigation Skip to main content Skip to primary sidebar The Programming Expert Solving All of Your Programming Headaches HTML JavaScript jQuery PHP Python SAS VBA About
Now add the value for the Century from the Century Table. If the Month is Jan. or Feb. and the Year is a leap year, subtract 1. [1] Add the results from the previous steps. If the resulting number is greater than 6, subtract the highest multiple of 7 in it. Using the resulting number, look up the day of the week in the Weekday-Table.
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@"EEEE"];
NSLog(@"%@", [dateFormatter stringFromDate:[NSDate date]]);
outputs current day of week as a string in locale dependent on current regional settings.
To get just a week day number you must use NSCalendar class:
NSCalendar *gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDateComponents *comps = [gregorian components:NSWeekdayCalendarUnit fromDate:[NSDate date]];
int weekday = [comps weekday];
Just use these three lines:
CFAbsoluteTime at = CFAbsoluteTimeGetCurrent();
CFTimeZoneRef tz = CFTimeZoneCopySystem();
SInt32 WeekdayNumber = CFAbsoluteTimeGetDayOfWeek(at, tz);
Many of the answers here are deprecated. This works as of iOS 8.4 and gives you the day of the week as a string and as a number.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"EEEE"];
NSLog(@"The day of the week: %@", [dateFormatter stringFromDate:[NSDate date]]);
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *comps = [gregorian components:NSCalendarUnitWeekday fromDate:[NSDate date]];
int weekday = [comps weekday];
NSLog(@"The week day number: %d", weekday);
Here's how you do it in Swift 3, and get a localised day name…
let dayNumber = Calendar.current.component(.weekday, from: Date()) // 1 - 7
let dayName = DateFormatter().weekdaySymbols[dayNumber - 1]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With