How do I get the day of the week as a string in Objective-C?
我如何在Objective-C中把一周的一天作为一个字符串?
13 个解决方案
#1
214
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];
#2
18
Just use these three lines:
只用这三行:
CFAbsoluteTime at = CFAbsoluteTimeGetCurrent();
CFTimeZoneRef tz = CFTimeZoneCopySystem();
SInt32 WeekdayNumber = CFAbsoluteTimeGetDayOfWeek(at, tz);
#3
14
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.
这里的许多答案都是不赞成的。这是ios8.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);
#4
7
-(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
7
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]
#6
3
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:
我需要一个简单的(Gregorian)的星期指数,其中0=Sunday和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;
}
#7
2
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())
#8
2
I think this topic is really useful, so I post some code Swift 2.1 compatible.
我认为这个主题非常有用,所以我发布了一些与Swift 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)
}
}
#9
1
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
# Date_Format_Patterns http://www.unicode.org/reports/tr35/tr35 - 25. - html
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文档中找到。
#10
1
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.
然后把工作日分配给结果的数字。
#11
0
I had quite strange issue with getting a day of week. Only setting firstWeekday wasn't enough. It was also necesarry to set the time zone. My working solution was:
我有一个很奇怪的问题,就是一周有一天。仅仅设定第一个工作日是不够的。设定时区也是必要的。我的工作的解决方案是:
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] ;
#12
0
Swift 2: Get day of week in one line. (based on neoscribe answer)
Swift 2:在一条线上获得一周中的一天。(基于neoscribe回答)
let dayOfWeek = Int((myDate.timeIntervalSinceReferenceDate / (60.0*60.0*24.0)) % 7)
let isMonday = (dayOfWeek == 0)
let isSunday = (dayOfWeek == 6)
#13
0
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
- e
- e
- c
- 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
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/
#1
214
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];
#2
18
Just use these three lines:
只用这三行:
CFAbsoluteTime at = CFAbsoluteTimeGetCurrent();
CFTimeZoneRef tz = CFTimeZoneCopySystem();
SInt32 WeekdayNumber = CFAbsoluteTimeGetDayOfWeek(at, tz);
#3
14
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.
这里的许多答案都是不赞成的。这是ios8.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);
#4
7
-(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
7
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]
#6
3
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:
我需要一个简单的(Gregorian)的星期指数,其中0=Sunday和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;
}
#7
2
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())
#8
2
I think this topic is really useful, so I post some code Swift 2.1 compatible.
我认为这个主题非常有用,所以我发布了一些与Swift 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)
}
}
#9
1
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
# Date_Format_Patterns http://www.unicode.org/reports/tr35/tr35 - 25. - html
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文档中找到。
#10
1
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.
然后把工作日分配给结果的数字。
#11
0
I had quite strange issue with getting a day of week. Only setting firstWeekday wasn't enough. It was also necesarry to set the time zone. My working solution was:
我有一个很奇怪的问题,就是一周有一天。仅仅设定第一个工作日是不够的。设定时区也是必要的。我的工作的解决方案是:
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] ;
#12
0
Swift 2: Get day of week in one line. (based on neoscribe answer)
Swift 2:在一条线上获得一周中的一天。(基于neoscribe回答)
let dayOfWeek = Int((myDate.timeIntervalSinceReferenceDate / (60.0*60.0*24.0)) % 7)
let isMonday = (dayOfWeek == 0)
let isSunday = (dayOfWeek == 6)
#13
0
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
- e
- e
- c
- 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
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/