ios 我如何通过 Foundation 获得一周中的哪一天?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4269093/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I get the day of the week with Foundation?
提问by Moshe
How do I get the day of the week as a string?
如何将星期几作为字符串获取?
回答by Vladimir
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 类:
NSCalendar *gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDateComponents *comps = [gregorian components:NSWeekdayCalendarUnit fromDate:[NSDate date]];
int weekday = [comps weekday];
回答by John Wilund
Just use these three lines:
只需使用这三行:
CFAbsoluteTime at = CFAbsoluteTimeGetCurrent();
CFTimeZoneRef tz = CFTimeZoneCopySystem();
SInt32 WeekdayNumber = CFAbsoluteTimeGetDayOfWeek(at, tz);
回答by George Steiner
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.
这里的许多答案已被弃用。这适用于 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);
回答by Ashley Mills
Here's how you do it in Swift 3, and get a localised day name…
以下是您在 Swift 3 中如何做到这一点,并获得本地化的日期名称……
let dayNumber = Calendar.current.component(.weekday, from: Date()) // 1 - 7
let dayName = DateFormatter().weekdaySymbols[dayNumber - 1]
回答by Ha cong Thuan
-(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);
}
回答by neoscribe
I needed a simple (Gregorian) day of the week index, where 0=Sunday and 6=Saturday to be used in pattern match algorithms. From there it is a simple matter of looking up the day name from an array using the index. Here is what I came up with that doesn't require date formatters, or NSCalendar or date component manipulation:
我需要一个简单的(公历)星期索引,其中 0=Sunday 和 6=Saturday 用于模式匹配算法。从那里开始,使用索引从数组中查找日期名称是一件简单的事情。这是我想出的不需要日期格式化程序、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;
}
回答by Luca Davanzo
I think this topic is really useful, so I post some code Swift 2.1compatible.
我觉得这个题目是真正有用的,所以我张贴一些代码雨燕2.1兼容。
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
斯威夫特 3.0
As @delta2flat suggested, I update answer giving user the ability to specify custom format.
正如@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)
}
}
回答by Janmenjaya
Here is the updated code for Swift 3
这是 Swift 3 的更新代码
Code :
代码 :
let calendar = Calendar(identifier: .gregorian)
let weekdayAsInteger = calendar.component(.weekday, from: Date())
To Print the name of the event as String:
将事件名称打印为字符串:
let dateFromat = DateFormatter()
datFormat.dateFormat = "EEEE"
let name = datFormat.string(from: Date())
回答by brainray
This way it works in Swift:
这样它在 Swift 中工作:
let calendar = NSCalendar.currentCalendar()
let weekday = calendar.component(.CalendarUnitWeekday, fromDate: NSDate())
Then assign the weekdays to the resulting numbers.
然后将工作日分配给结果数字。
回答by EquiAvia Tech
Vladimir's answer worked well for me, but I thought that I would post the Unicode link for the date format strings.
Vladimir 的回答对我很有效,但我认为我会发布日期格式字符串的 Unicode 链接。
http://www.unicode.org/reports/tr35/tr35-25.html#Date_Format_Patterns
http://www.unicode.org/reports/tr35/tr35-25.html#Date_Format_Patterns
This link is for iOS 6. The other versions of iOS have different standards which can be found in the X-Code documentation.
此链接适用于 iOS 6。其他版本的 iOS 有不同的标准,可以在 X-Code 文档中找到。