目标C-如何获得NSDate的工作日? [英] Objective C - How can i get the weekday from NSDate?
问题描述
我需要能够从nsdate获得工作日,我有以下代码,并且它总是返回1.我试图更改月份,我尝试将所有内容从1改为12,但工作日的结果始终为1
I need to be able to get the weekday from nsdate, i have the following code and it always return 1. I tried to change the month, i tried everything from 1 to 12 but the result of the week day is always 1.
NSDate *date2 = [[NSDate alloc] initWithString:[NSString stringWithFormat:@"%d-%d-%d", 2010, 6, 1]];
unsigned units2 = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit;
NSCalendar *calendar2 = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components2 = [calendar2 components:units2 fromDate:date2];
int startWeekDay = [components2 weekday];
[date2 release];
[calendar2 release];
推荐答案
为Mac
根据文档,字符串格式必须为 —YYYY-MM-DD HH:MM:SS±HHMM ,所有字段均为必填
The format of the string has to be —YYYY-MM-DD HH:MM:SS ±HHMM according to the docs, all fields mandatory
旧答案(适用于iPhone,并在模拟器上进行了测试):
NSDate
中没有(公共)-initWithString:
方法,返回的结果是 not 您期望的结果.
There is no (public) -initWithString:
method in NSDate
, and what you get returned is not what you expect.
使用正确配置的(需要提供输入格式)NSDateFormatter
和-dateFromString:
.
Use a properly configured (you need to give the input format) NSDateFormatter
and -dateFromString:
.
这篇关于目标C-如何获得NSDate的工作日?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!