NSDate 得到一周中的所有短天?
NSDate get all short days of the week?
我需要在短模式下获取一周中的所有日子,如下所示:
sun,mon,etc ..
我试过这个:
NSDateFormatter * df = [[NSDateFormatter alloc] init];
[df setLocale:[[NSLocale alloc] initWithLocaleIdentifier:[[NSLocale preferredLanguages] objectAtIndex:0]]];
[df setDateFormat:@"c"];
总是给出全名,怎么弄短的?
一种选择是使用 ccc
[df setDateFormat:@"ccc"];
Stand-Alone local day of week - Use one letter for the local numeric value (same as 'e'), three for the short day, four for the full name, five for the narrow name, or six for the short name.
或者使用 eee
或 E
- 不确定究竟有什么区别。
Day of week - Use one through three letters for the short day, or four for the full name, five for the narrow name, or six for the short name.
或
Local day of week. Same as E except adds a numeric value that will depend on the local starting day of the week, using one or two letters. For this example, Monday is the first day of the week.
星期几,采用 3 个字符的短格式,例如 'Mon'、'Tue'、'Wed'、...
NSDate *date = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"EEE"];
NSString *strDate = [formatter stringFromDate:date];
或(针对您的代码)
NSDateFormatter * df = [[NSDateFormatter alloc] init];
[df setLocale:[[NSLocale alloc] initWithLocaleIdentifier:[[NSLocale preferredLanguages] objectAtIndex:0]]];
[df setDateFormat:@"EEE"];
使用 shortWeekdaySymbols,它给出了一个名称数组
#import <Foundation/Foundation.h>
int main(int argc, char *argv[]) {
@autoreleasepool {
NSDateFormatter * df = [[NSDateFormatter alloc] init];
NSLog(@"%@ \n OR \n %@", df.weekdaySymbols, df. df.shortWeekdaySymbols);
}
}
无需使用任何格式,除非您实际上不是在查找星期而是在查找单个日期:
How do I get the day of the week with Cocoa Touch?
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
NSArray * shortWeeks = [formatter shortWeekdaySymbols];
NSLog(@"%@",shortWeeks);
日志
2015-07-14 17:25:31.470 NewOcTest[9016:286325] (
Sun,
Mon,
Tue,
Wed,
Thu,
Fri,
Sat
)
此外,dateFormatter
中还有veryShortWeekdaySymbols
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
NSArray * shortWeeks = [formatter veryShortWeekdaySymbols];
NSLog(@"%@",shortWeeks);
日志
2015-07-14 17:30:30.686 NewOcTest[9073:290513] (
S,
M,
T,
W,
T,
F,
S
)
我需要在短模式下获取一周中的所有日子,如下所示:
sun,mon,etc ..
我试过这个:
NSDateFormatter * df = [[NSDateFormatter alloc] init];
[df setLocale:[[NSLocale alloc] initWithLocaleIdentifier:[[NSLocale preferredLanguages] objectAtIndex:0]]];
[df setDateFormat:@"c"];
总是给出全名,怎么弄短的?
一种选择是使用 ccc
[df setDateFormat:@"ccc"];
Stand-Alone local day of week - Use one letter for the local numeric value (same as 'e'), three for the short day, four for the full name, five for the narrow name, or six for the short name.
或者使用 eee
或 E
- 不确定究竟有什么区别。
Day of week - Use one through three letters for the short day, or four for the full name, five for the narrow name, or six for the short name.
或
Local day of week. Same as E except adds a numeric value that will depend on the local starting day of the week, using one or two letters. For this example, Monday is the first day of the week.
星期几,采用 3 个字符的短格式,例如 'Mon'、'Tue'、'Wed'、...
NSDate *date = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"EEE"];
NSString *strDate = [formatter stringFromDate:date];
或(针对您的代码)
NSDateFormatter * df = [[NSDateFormatter alloc] init];
[df setLocale:[[NSLocale alloc] initWithLocaleIdentifier:[[NSLocale preferredLanguages] objectAtIndex:0]]];
[df setDateFormat:@"EEE"];
使用 shortWeekdaySymbols,它给出了一个名称数组
#import <Foundation/Foundation.h>
int main(int argc, char *argv[]) {
@autoreleasepool {
NSDateFormatter * df = [[NSDateFormatter alloc] init];
NSLog(@"%@ \n OR \n %@", df.weekdaySymbols, df. df.shortWeekdaySymbols);
}
}
无需使用任何格式,除非您实际上不是在查找星期而是在查找单个日期: How do I get the day of the week with Cocoa Touch?
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
NSArray * shortWeeks = [formatter shortWeekdaySymbols];
NSLog(@"%@",shortWeeks);
日志
2015-07-14 17:25:31.470 NewOcTest[9016:286325] (
Sun,
Mon,
Tue,
Wed,
Thu,
Fri,
Sat
)
此外,dateFormatter
veryShortWeekdaySymbols
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
NSArray * shortWeeks = [formatter veryShortWeekdaySymbols];
NSLog(@"%@",shortWeeks);
日志
2015-07-14 17:30:30.686 NewOcTest[9073:290513] (
S,
M,
T,
W,
T,
F,
S
)