Answers:
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@"EEEE"];
NSLog(@"%@", [dateFormatter stringFromDate:[NSDate date]]);
根据当前区域设置,以语言环境的字符串形式输出当前星期几。
要获取一个工作日的编号,您必须使用NSCalendar类:
NSCalendar *gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDateComponents *comps = [gregorian components:NSWeekdayCalendarUnit fromDate:[NSDate date]];
int weekday = [comps weekday];
只需使用以下三行:
CFAbsoluteTime at = CFAbsoluteTimeGetCurrent();
CFTimeZoneRef tz = CFTimeZoneCopySystem();
SInt32 WeekdayNumber = CFAbsoluteTimeGetDayOfWeek(at, tz);
不建议使用此处的许多答案。自iOS 8.4起可以使用,并以字符串和数字的形式显示星期几。
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);
weekday
组成部分NSCalendar
是NSInteger
,如果需要,您需要将其强制[comps weekday]
转换为int
,否则它将显示有关此的警告。
这是您在Swift 3中执行的操作,并获得了本地化的日期名称…
let dayNumber = Calendar.current.component(.weekday, from: Date()) // 1 - 7
let dayName = DateFormatter().weekdaySymbols[dayNumber - 1]
-(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);
}
我需要一个简单的(星期几)星期几索引,其中0 =星期日和6 =星期六要用于模式匹配算法。从那里开始,很简单的事情就是使用索引从数组中查找日期名称。这是我想出的不需要日期格式化程序或NSCalendar或日期组件操作的内容:
+(long)dayOfWeek:(NSDate *)anyDate {
//calculate number of days since reference date jan 1, 01
NSTimeInterval utcoffset = [[NSTimeZone localTimeZone] secondsFromGMT];
NSTimeInterval interval = ([anyDate timeIntervalSinceReferenceDate]+utcoffset)/(60.0*60.0*24.0);
//mod 7 the number of days to identify day index
long dayix=((long)interval+8) % 7;
return dayix;
}
我认为这个主题真的很有用,因此我发布了一些与Swift 2.1兼容的代码。
extension NSDate {
static func getBeautyToday() -> String {
let now = NSDate()
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "EEEE',' dd MMMM"
return dateFormatter.stringFromDate(now)
}
}
您可以致电的任何地方:
let today = NSDate.getBeautyToday()
print(today) ---> "Monday, 14 December"
斯威夫特3.0
正如@ delta2flat所建议的那样,我更新了答案,使用户能够指定自定义格式。
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)
}
}
弗拉基米尔的答案对我来说效果很好,但我认为我应该为日期格式字符串发布Unicode链接。
http://www.unicode.org/reports/tr35/tr35-25.html#Date_Format_Patterns
该链接适用于iOS6。其他版本的iOS具有不同的标准,可以在X代码文档中找到这些标准。
我有一个很奇怪的问题是每天有一天。仅设置firstWeekday是不够的。设置时区也是必要的。我的工作解决方案是:
NSCalendar* cal = [NSCalendar currentCalendar];
[cal setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
[cal setFirstWeekday:1]; //Sunday
NSDateComponents* comp = [cal components:( NSWeekOfMonthCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit | NSWeekCalendarUnit) fromDate:date];
return [comp weekday] ;
Swift 2:每周一行。(基于新手答案)
let dayOfWeek = Int((myDate.timeIntervalSinceReferenceDate / (60.0*60.0*24.0)) % 7)
let isMonday = (dayOfWeek == 0)
let isSunday = (dayOfWeek == 6)
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";
我们可以使用三种符号来格式化星期几:
以下两个文档可能会对您有所帮助。
http://www.unicode.org/reports/tr35/tr35-31/tr35-dates.html#Date_Format_Patterns
演示:
您可以在此网站上测试您的模式:
[dateFormatter weekdaySymbols]
(以及类似),其在指数0开始返回星期日NSString的的一个NSArray