NSDate NSDate 的组件

发布于 2024-11-17 07:05:05 字数 85 浏览 4 评论 0原文

如何获取单个 NSDate 的 NSDateComponents ?我不想要两个日期之间差异的组成部分,只想要 NSDate 的秒、分钟、小时、日、月和年?

How do I get the NSDateComponents of a single NSDate? I don't want the components of the difference between 2 dates, just the seconds, minutes, hours, day, month and year of a NSDate?

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

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

发布评论

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

评论(4

回心转意 2024-11-24 07:05:05

中有一个示例Apple 文档,清单 3:获取日期的组成部分

NSDate *today = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc]
                         initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *weekdayComponents =
                    [gregorian components:(NSDayCalendarUnit | 
                                           NSWeekdayCalendarUnit) fromDate:today];
NSInteger day = [weekdayComponents day];
NSInteger weekday = [weekdayComponents weekday];

请注意,您必须将 日历单位< /a> 您想要在 components 方法的调用中。

There's an example in the Apple docs, Listing 3: Getting a date’s components:

NSDate *today = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc]
                         initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *weekdayComponents =
                    [gregorian components:(NSDayCalendarUnit | 
                                           NSWeekdayCalendarUnit) fromDate:today];
NSInteger day = [weekdayComponents day];
NSInteger weekday = [weekdayComponents weekday];

Note that you have to 'or' together the calendar units you want in the call of the components method.

客…行舟 2024-11-24 07:05:05

使用 NSCalendar 的方法:

- (NSDateComponents *)components:(NSUInteger)unitFlags fromDate:(NSDate *)date

如下:

unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit;
NSDate *date = [NSDate date];


NSCalendar * cal = [NSCalendar currentCalendar];
NSDateComponents *comps = [cal components:unitFlags fromDate:date];

using NSCalendar's method:

- (NSDateComponents *)components:(NSUInteger)unitFlags fromDate:(NSDate *)date

like:

unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit;
NSDate *date = [NSDate date];


NSCalendar * cal = [NSCalendar currentCalendar];
NSDateComponents *comps = [cal components:unitFlags fromDate:date];
捎一片雪花 2024-11-24 07:05:05

根据Apple开发者文档, DateComponents分钟小时实例属性。以下 Swift 5 Playground 代码展示了如何使用 DateComponentsDate 实例获取秒、分钟、小时、日、月和年组件:

import Foundation

var calendar = Calendar(identifier: .gregorian)
calendar.timeZone = TimeZone(abbreviation: "CET")!
let date = Date()

// Get seconds, minutes, hours, day, month and year
let components = calendar.dateComponents([.second, .minute, .hour, .day, .month, .year], from: date)
let seconds = components.second
let minutes = components.minute
let hours = components.hour
let day = components.day
let month = components.month
let year = components.year

// Print components
print(String(describing: seconds)) // prints Optional(33)
print(String(describing: minutes)) // prints Optional(42)
print(String(describing: hours)) // prints Optional(22)
print(String(describing: day)) // prints Optional(17)
print(String(describing: month)) // prints Optional(12)
print(String(describing: year)) // prints Optional(2016)

/* ... */

// Set a formatter in order to display date
let formatter = DateFormatter()
formatter.dateStyle = .short
formatter.timeStyle = .long
formatter.timeZone = TimeZone(abbreviation: "CET")
formatter.locale = Locale(identifier: "fr_FR")
print(formatter.string(from: date)) // prints 17/12/2016 22:42:33 UTC+1

According to Apple Developer documentation, DateComponents has second, minute, hour, day, month, year instance properties. The following Swift 5 Playground code shows how to get seconds, minutes, hours, day, month and year components from a Date instance using DateComponents:

import Foundation

var calendar = Calendar(identifier: .gregorian)
calendar.timeZone = TimeZone(abbreviation: "CET")!
let date = Date()

// Get seconds, minutes, hours, day, month and year
let components = calendar.dateComponents([.second, .minute, .hour, .day, .month, .year], from: date)
let seconds = components.second
let minutes = components.minute
let hours = components.hour
let day = components.day
let month = components.month
let year = components.year

// Print components
print(String(describing: seconds)) // prints Optional(33)
print(String(describing: minutes)) // prints Optional(42)
print(String(describing: hours)) // prints Optional(22)
print(String(describing: day)) // prints Optional(17)
print(String(describing: month)) // prints Optional(12)
print(String(describing: year)) // prints Optional(2016)

/* ... */

// Set a formatter in order to display date
let formatter = DateFormatter()
formatter.dateStyle = .short
formatter.timeStyle = .long
formatter.timeZone = TimeZone(abbreviation: "CET")
formatter.locale = Locale(identifier: "fr_FR")
print(formatter.string(from: date)) // prints 17/12/2016 22:42:33 UTC+1
心凉 2024-11-24 07:05:05

在 Swift 2.x 中,你想要的组件是这样传递的。

let gregorianCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian) 
let weekdayHourMinutes = gregorianCalendar?.components([.Weekday, .Hour, .Minute], fromDate: date)

in Swift 2.x the components you want are passed like this.

let gregorianCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian) 
let weekdayHourMinutes = gregorianCalendar?.components([.Weekday, .Hour, .Minute], fromDate: date)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文