Objective C - 如何从 NSDate 获取工作日?

发布于 2024-09-15 07:33:18 字数 565 浏览 12 评论 0原文

我需要能够从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

沫离伤花 2024-09-22 07:33:18

创建一个仅包含工作日的 NSDateFormatter 将执行您想要的操作。

NSDate *now = [NSDate date];
NSDateFormatter *weekday = [[[NSDateFormatter alloc] init] autorelease];
[weekday setDateFormat: @"EEEE"];
NSLog(@"The day of the week is: %@", [weekday stringFromDate:now]);

如果您需要国际化,可以向 NSDateFormatter 发送语言环境,这将提供正确的翻译。

日期格式化程序通过以下标准进行控制: Unicode 日期格式

Creating an NSDateFormatter that only contains the weekday will do what you want.

NSDate *now = [NSDate date];
NSDateFormatter *weekday = [[[NSDateFormatter alloc] init] autorelease];
[weekday setDateFormat: @"EEEE"];
NSLog(@"The day of the week is: %@", [weekday stringFromDate:now]);

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

夏花。依旧 2024-09-22 07:33:18

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 in NSDate, and what you get returned is not what you expect.

Use a properly configured (you need to give the input format) NSDateFormatter and -dateFromString:.

不爱素颜 2024-09-22 07:33:18

我解决了问题,问题是我的日期字符串的格式错误:

NSDate *date = [[NSDate alloc] initWithString:[NSString stringWithFormat:@"%d-%d-%d 10:45:32 +0600", selectedYear, selectedMonth, selectedDay]];

并且因为我不关心时间,所以我随机将时间传递到字符串的末尾

I solved the problem, The problem was that the format of my date string was wrong:

NSDate *date = [[NSDate alloc] initWithString:[NSString stringWithFormat:@"%d-%d-%d 10:45:32 +0600", selectedYear, selectedMonth, selectedDay]];

and since i don't care about the time i randomly pass a time to the end of the string

可爱暴击 2024-09-22 07:33:18

我使用的是 Xcode 6.4

有很多方法可以设置 NSDate。我不会在这里讨论这个。您已经通过一种方式完成了它,使用字符串(我避免使用这种方法,因为它很容易出错),而我正在以另一种方式设置它。无论如何,访问您的组件 weekday 或 setDay 方法。

NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
[calendar setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en-US"]];
NSDateComponents *components = [calendar components:(NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitWeekday|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond) fromDate:[NSDate date]];
    NSDate *date = [calendar dateFromComponents:components];

这样,您就可以像这样获取或设置这些组件中的任何一个:

[components weekday] //to get, then doSomething
[components setDay:6]; //to set

当然,仅在更改组件后才设置 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.

NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
[calendar setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en-US"]];
NSDateComponents *components = [calendar components:(NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitWeekday|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond) fromDate:[NSDate date]];
    NSDate *date = [calendar dateFromComponents:components];

this way, you can get or set any of these components like this:

[components weekday] //to get, then doSomething
[components setDay:6]; //to set

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.

万劫不复 2024-09-22 07:33:18

任何来这里寻找更新的 Swift 4 解决方案的人:

extension Date {

    func getDayOfWeek() -> String {
        let dateFormatter = DateFormatter()
        dateFormatter.timeZone = TimeZone.current
        dateFormatter.locale = Locale(identifier: "en_US")
        dateFormatter.setLocalizedDateFormatFromTemplate("EEEE")

        return dateFormatter.string(from: self)
    }
}

let inputString = "2018-04-16T15:47:39.000Z"

let result = inputString.getDayOfWeek()

result = 星期一

Anyone coming here looking for a more recent Swift 4 solution:

extension Date {

    func getDayOfWeek() -> String {
        let dateFormatter = DateFormatter()
        dateFormatter.timeZone = TimeZone.current
        dateFormatter.locale = Locale(identifier: "en_US")
        dateFormatter.setLocalizedDateFormatFromTemplate("EEEE")

        return dateFormatter.string(from: self)
    }
}

let inputString = "2018-04-16T15:47:39.000Z"

let result = inputString.getDayOfWeek()

result = Monday

笙痞 2024-09-22 07:33:18

Swift 3 中投票最多的答案。

let formatter = DateFormatter()
formatter.dateFormat = "EEEE"
print("The day of the week is \(formatter.string(from: Date()))")

Most upvoting answer in Swift 3.

let formatter = DateFormatter()
formatter.dateFormat = "EEEE"
print("The day of the week is \(formatter.string(from: Date()))")
烂人 2024-09-22 07:33:18

我使用以下网站帮助设置字符串以检索我在约会时想要的格式

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文