如何获得Cocoa Touch的星期几? [英] How do I get the day of the week with Cocoa Touch?

查看:103
本文介绍了如何获得Cocoa Touch的星期几?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Objective-C中将星期几作为字符串获取?

How do I get the day of the week as a string in Objective-C?

推荐答案

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.

要获取一个星期几的数字,您必须使用NSCalendar类:

To get just a week day number you must use NSCalendar class:

NSCalendar *gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDateComponents *comps = [gregorian components:NSWeekdayCalendarUnit fromDate:[NSDate date]];
int weekday = [comps weekday];

这篇关于如何获得Cocoa Touch的星期几?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆