如何使用 NSDate 从今天的日期查找工作日?

发布于 2024-08-25 17:22:28 字数 167 浏览 1 评论 0原文

我知道我可以通过 [NSDate date]; 找出今天的日期,但是我如何找出今天是一周中的哪一天,例如星期六、星期五等。

我知道 %w code> 可以在 NSDateFormatter 中使用,但我不知道如何使用它。

I know that I can figure out today's date by [NSDate date]; but how would I find out today day of the week, like Saturday, Friday etc.

I know that %w can be used in NSDateFormatter, but I don't know to use it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

把人绕傻吧 2024-09-01 17:22:28
NSCalendar* cal = [NSCalendar currentCalendar];
NSDateComponents* comp = [cal components:NSCalendarUnitWeekday fromDate:[NSDate date]];
return [comp weekday]; // 1 = Sunday, 2 = Monday, etc.

如果您只需要工作日而不需要其他组件(需要 iOS 8+@HuguesBR 的回答>)。

NSInteger weekday = [[NSCalendar currentCalendar] component:NSCalendarUnitWeekday 
                                                   fromDate:[NSDate date]];

(如果您没有得到正确答案,请检查您是否将 NSCalendarUnitWeekday 与其他与周相关的组件(如 NSCalendarUnitWeekdayOrdinal)输入错误> 等)


Swift 3:

let weekday = Calendar.current.component(.weekday, from: Date())
// 1 = Sunday, 2 = Monday, etc.
NSCalendar* cal = [NSCalendar currentCalendar];
NSDateComponents* comp = [cal components:NSCalendarUnitWeekday fromDate:[NSDate date]];
return [comp weekday]; // 1 = Sunday, 2 = Monday, etc.

See @HuguesBR's answer if you just need the weekday without other components (requires iOS 8+).

NSInteger weekday = [[NSCalendar currentCalendar] component:NSCalendarUnitWeekday 
                                                   fromDate:[NSDate date]];

(If you don't get a correct answer, check if you have mistyped NSCalendarUnitWeekday with other week-related components like NSCalendarUnitWeekdayOrdinal, etc.)


Swift 3:

let weekday = Calendar.current.component(.weekday, from: Date())
// 1 = Sunday, 2 = Monday, etc.
辞取 2024-09-01 17:22:28

如果有人对 Swift 解决方案感兴趣,请使用:

import Foundation

let weekday = NSCalendar.current.component(.weekday, from: Date())
print(weekday) // => prints the weekday number 1-7 (Sunday through Saturday) 

请参阅演示

更新:已修复以在 Swift 3 中工作

If someone is interested in a Swift solution, then use:

import Foundation

let weekday = NSCalendar.current.component(.weekday, from: Date())
print(weekday) // => prints the weekday number 1-7 (Sunday through Saturday) 

See the demo.

Update: fixed to work in Swift 3

真心难拥有 2024-09-01 17:22:28

这是适用于 iOS 8 的更新版本

NSCalendar* currentCalendar = [NSCalendar currentCalendar];
NSDateComponents* dateComponents = [currentCalendar components:NSWeekdayCalendarUnit fromDate:[NSDate date]];
return [dateComponents weekday];

This is updated version valid for iOS 8

NSCalendar* currentCalendar = [NSCalendar currentCalendar];
NSDateComponents* dateComponents = [currentCalendar components:NSWeekdayCalendarUnit fromDate:[NSDate date]];
return [dateComponents weekday];
夏夜暖风 2024-09-01 17:22:28

较短的版本

NSCalendarUnit dayOfTheWeek = [[NSCalendar currentCalendar] component:NSCalendarUnitWeekday fromDate:yourDate];

Shorter version

NSCalendarUnit dayOfTheWeek = [[NSCalendar currentCalendar] component:NSCalendarUnitWeekday fromDate:yourDate];
海的爱人是光 2024-09-01 17:22:28
let today = Date()
let day = Calendar.current.dateComponents([.weekday], from: today).weekday
if ( day == 5)
    {
        yourText.text = "Thursday"
    }
let today = Date()
let day = Calendar.current.dateComponents([.weekday], from: today).weekday
if ( day == 5)
    {
        yourText.text = "Thursday"
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文