[英]Day Name From NSDate?
I would like to show the name of day in my iPhone application and i don't found the solution. 我想在我的iPhone应用程序中显示日期的名称,但我找不到解决方案。 Thanks for help
感谢帮助
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"EEEE"];
NSString *dayName = [dateFormatter stringFromDate:yourDate];
[dateFormatter release];
You get dayName
in the locale of the user. 您在用户的语言环境中获得
dayName
。
(check Unicode standards for date formats samples) (检查日期格式样本的Unicode标准 )
I found it, the answer was : 我找到了,答案是:
NSDate *now = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"EEEE"];
NSLog(@"%@",[dateFormatter stringFromDate:now]);
Thanks 谢谢
NSDate category NSDate类别
+ (NSString *)dayNameWith0Monday:(NSInteger)index {
static NSDateFormatter * DateFormatter = nil;
if (DateFormatter == nil) {
DateFormatter = [[NSDateFormatter alloc] init];
[DateFormatter setDateFormat:@"EEEE"];
[DateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
}
NSDate * day = [NSDate dateWithTimeIntervalSince1970:((4 * 24 * 60 * 60) + (24 * 60 * 60 * index))];
return [DateFormatter stringFromDate:day];
}
0 will always be Monday! 0永远是星期一! in case you need such behaviour
如果你需要这样的行为
Look at -[NSCalendar components:fromDate:]. 看看 - [NSCalendar组件:fromDate:]。
A date by itself doesn't have a day, because it may have different days in different calendars (Gregorian, Chinese, etc.). 日期本身没有一天,因为它可能在不同的日历(格里高利,中国等)中有不同的日子。
EDIT: actually, sorry. 编辑:实际上,抱歉。 That's what you would do to get the day and work with it programmatically.
这就是你想要的一天,并以编程方式使用它。 If you only want to display the day, look at NSDateFormatter.
如果您只想显示日期,请查看NSDateFormatter。
@Jilouc answer in Swift: @Jilouc在Swift中回答:
let formatter = DateFormatter.init()
formatter.dateFormat = "EEEE"
let date = Date.init() //or any date
let dayName = formatter.string(from: date)
有很多很好的日期和时间处理资源 - 这是我从github上学到的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.