How do I get the day of the week with Foundation?

2019-01-02 17:37发布

How do I get the day of the week as a string?

13条回答
与风俱净
2楼-- · 2019-01-02 18:09

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);
查看更多
浪荡孟婆
3楼-- · 2019-01-02 18:12

Just use these three lines:

CFAbsoluteTime at = CFAbsoluteTimeGetCurrent();
CFTimeZoneRef tz = CFTimeZoneCopySystem();
SInt32 WeekdayNumber = CFAbsoluteTimeGetDayOfWeek(at, tz);
查看更多
浅入江南
4楼-- · 2019-01-02 18:13
-(void)getdate {
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"yyyy-MM-dd"];
    NSDateFormatter *format = [[NSDateFormatter alloc] init];
    [format setDateFormat:@"MMM dd, yyyy HH:mm"];
    NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
    [timeFormat setDateFormat:@"HH:mm:ss"];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init] ;
    [dateFormatter setDateFormat:@"EEEE"];

    NSDate *now = [[NSDate alloc] init];
    NSString *dateString = [format stringFromDate:now];
    NSString *theDate = [dateFormat stringFromDate:now];
    NSString *theTime = [timeFormat stringFromDate:now];

    NSString *week = [dateFormatter stringFromDate:now];
    NSLog(@"\n"
          "theDate: |%@| \n"
          "theTime: |%@| \n"
          "Now: |%@| \n"
          "Week: |%@| \n"
         , theDate, theTime,dateString,week); 
}
查看更多
临风纵饮
5楼-- · 2019-01-02 18:16

I think this topic is really useful, so I post some code Swift 2.1 compatible.

extension NSDate {

    static func getBeautyToday() -> String {
       let now = NSDate()
       let dateFormatter = NSDateFormatter()
       dateFormatter.dateFormat = "EEEE',' dd MMMM"
       return dateFormatter.stringFromDate(now)
    }

}

Anywhere you can call:

let today = NSDate.getBeautyToday()
print(today) ---> "Monday, 14 December"

Swift 3.0

As @delta2flat suggested, I update answer giving user the ability to specify custom format.

extension NSDate {

    static func getBeautyToday(format: String = "EEEE',' dd MMMM") -> String {
        let now = Date()
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = format
        return dateFormatter.string(from: now)
    }

}
查看更多
像晚风撩人
6楼-- · 2019-01-02 18:17

Swift 2: Get day of week in one line. (based on neoscribe answer)

let dayOfWeek  = Int((myDate.timeIntervalSinceReferenceDate / (60.0*60.0*24.0)) % 7)
let isMonday   = (dayOfWeek == 0)
let isSunday   = (dayOfWeek == 6)
查看更多
后来的你喜欢了谁
7楼-- · 2019-01-02 18:20
self.dateTimeFormatter = [[NSDateFormatter alloc] init];
self.dateTimeFormatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0]; // your timezone
self.dateTimeFormatter.locale = [NSLocale localeWithLocaleIdentifier:@"zh_CN"]; // your locale
self.dateTimeFormatter.dateFormat = @"ccc MM-dd mm:ss";

there are three symbols we can use to format day of week:

  • E
  • e
  • c

The following two documents may help you.

https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html

http://www.unicode.org/reports/tr35/tr35-31/tr35-dates.html#Date_Format_Patterns

Demo:

you can test your pattern on this website:

http://nsdateformatter.com/

查看更多
登录 后发表回答