Objective C - 如何从 NSDate 获取工作日?
我需要能够从 nsdate 获取工作日,我有以下代码,它总是返回 1。我尝试更改月份,我尝试了从 1 到 12 的所有内容,但工作日的结果始终是 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];
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];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
创建一个仅包含工作日的
NSDateFormatter
将执行您想要的操作。如果您需要国际化,可以向
NSDateFormatter
发送语言环境,这将提供正确的翻译。日期格式化程序通过以下标准进行控制: Unicode 日期格式
Creating an
NSDateFormatter
that only contains the weekday will do what you want.If you need internationalization, the
NSDateFormatter
can be sent a locale, which will give the proper translation.The date formatters are controlled through this standard: Unicode Date Formats
Mac 编辑:
根据文档,字符串的格式必须为 —YYYY-MM-DD HH:MM:SS ±HHMM,所有字段均为必填
旧答案(适用于 iPhone 并在模拟器上测试):
NSDate 中没有(公共)
-initWithString:
方法,并且返回的是 不是您所期望的。使用正确配置的(需要给出输入格式)
NSDateFormatter
和-dateFromString:
。Edit for Mac:
The format of the string has to be —YYYY-MM-DD HH:MM:SS ±HHMM according to the docs, all fields mandatory
Old answer (for iPhone and tested on simulator):
There is no (public)
-initWithString:
method inNSDate
, and what you get returned is not what you expect.Use a properly configured (you need to give the input format)
NSDateFormatter
and-dateFromString:
.我解决了问题,问题是我的日期字符串的格式错误:
并且因为我不关心时间,所以我随机将时间传递到字符串的末尾
I solved the problem, The problem was that the format of my date string was wrong:
and since i don't care about the time i randomly pass a time to the end of the string
我使用的是 Xcode 6.4
有很多方法可以设置 NSDate。我不会在这里讨论这个。您已经通过一种方式完成了它,使用字符串(我避免使用这种方法,因为它很容易出错),而我正在以另一种方式设置它。无论如何,访问您的组件 weekday 或 setDay 方法。
这样,您就可以像这样获取或设置这些组件中的任何一个:
当然,仅在更改组件后才设置 NSDate *date = [calendar dateFromComponents:components]; 。
我添加了额外的本地组件和其他上下文组件,以防您也想设置它们。
这是免费代码,不要敲它。
I'm using Xcode 6.4
There are many ways to setup an NSDate. I won't go over that here. You've done it one way, using a string (A method I avoid due to it being very vulnerable to error), and I'm setting it another way. Regardless, access your components weekday or setDay methods.
this way, you can get or set any of these components like this:
and, of course, set
NSDate *date = [calendar dateFromComponents:components];
only after you've changed the components.I added in the extra local and other components for context in case you'd like to set those too.
It's free code, don't knock it.
任何来这里寻找更新的 Swift 4 解决方案的人:
let inputString = "2018-04-16T15:47:39.000Z"
let result = inputString.getDayOfWeek()
result =
星期一
Anyone coming here looking for a more recent Swift 4 solution:
let inputString = "2018-04-16T15:47:39.000Z"
let result = inputString.getDayOfWeek()
result =
Monday
Swift 3 中投票最多的答案。
Most upvoting answer in Swift 3.
我使用以下网站帮助设置字符串以检索我在约会时想要的格式
Coder Wall - Objective C 日期格式指南
I used the following website which helped with setting up the string to retrieve the format I wanted on my date
Coder Wall - Guide to objective c date formatting