如何将当前日期(或任何日期)创建为没有小时、分钟和秒的 NSDate?

发布于 2024-10-15 08:25:23 字数 397 浏览 2 评论 0原文

我需要创建当前日期(或任何 NSDate)的 NSDate ,不带小时、分钟和秒,并将其保留为 NSDate,只需几行可能的?

我需要对日期进行一些计算,并且小时、分钟和秒会引起问题,我需要绝对日期(我希望绝对是正确的短语)。它适用于 iPhone 应用程序。

我正在使用 [NSDate date] 创建日期,其中添加了小时、分钟和秒。另外,我还为日期添加了月份,我认为这可以满足夏令时的要求,因为在某些日期我有 2300 小时。

因此,理想情况下,我需要一个函数来从 NSDates 创建绝对 NSDates。

我知道我之前问过类似的问题,但我需要最终得到 NSDate 而不是字符串,而且我有点担心指定日期格式,例如 yyyy 等。

I need to create an NSDate of the current date (or on any NSDate) without the hours, minutes and seconds and keep it as an NSDate, in as few lines of as possible?

I need to perform some calculations on dates and having hours, minutes and seconds cause problems, I need absolute dates (I hope absolute is the right phrase). Its for an iPhone app.

I'm creating dates with [NSDate date] which add hours, minutes and seconds. Also I'm adding months to dates which I think caters for day light savings as I get 2300 hours on some dates.

So ideally I need a function to create absolute NSDates from NSDates.

I know I asked a similar question earlier, but I need to end up with NSDate not a string and I'm a little concerned about specifying a date format e.g. yyyy etc.

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

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

发布评论

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

评论(2

一指流沙 2024-10-22 08:25:23

首先,您必须了解,即使从 NSDate 中删除小时、分钟和秒,它仍然代表单个时刻。您永远不会得到像 2011-01-28 这样代表一整天的 NSDate ;它将始终为 2011-01-28 00:00:00 +00:00。这意味着您还必须考虑时区。

下面是一个方法(作为 NSDate 类别实现),它在 UTC 午夜与 self 同一天返回一个 NSDate

- (NSDate *)midnightUTC {
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    [calendar setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];

    NSDateComponents *dateComponents = [calendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit
                                                   fromDate:self];
    [dateComponents setHour:0];
    [dateComponents setMinute:0];
    [dateComponents setSecond:0];

    NSDate *midnightUTC = [calendar dateFromComponents:dateComponents];
    [calendar release];

    return midnightUTC;
}

First, you have to understand that even if you strip hours, minutes and seconds from an NSDate, it will still represent a single moment in time. You will never get an NSDate to represent an entire day like 2011-01-28; it will always be 2011-01-28 00:00:00 +00:00. That implies that you have to take time zones into account, as well.

Here is a method (to be implemented as an NSDate category) that returns an NSDate at midnight UTC on the same day as self:

- (NSDate *)midnightUTC {
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    [calendar setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];

    NSDateComponents *dateComponents = [calendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit
                                                   fromDate:self];
    [dateComponents setHour:0];
    [dateComponents setMinute:0];
    [dateComponents setSecond:0];

    NSDate *midnightUTC = [calendar dateFromComponents:dateComponents];
    [calendar release];

    return midnightUTC;
}
思念满溢 2024-10-22 08:25:23
NSDate *oldDate = [NSDate date];
NSCalendarUnit requiredDateComponents = NSYearCalendarUnit| NSMonthCalendarUnit | NSDayCalendarUnit;    
NSDateComponents *components = [[NSCalendar currentCalendar] components:requiredDateComponents fromDate:oldDate]; 
NSDate *newDate = [[NSCalendar currentCalendar] dateFromComponents:components];
NSDate *oldDate = [NSDate date];
NSCalendarUnit requiredDateComponents = NSYearCalendarUnit| NSMonthCalendarUnit | NSDayCalendarUnit;    
NSDateComponents *components = [[NSCalendar currentCalendar] components:requiredDateComponents fromDate:oldDate]; 
NSDate *newDate = [[NSCalendar currentCalendar] dateFromComponents:components];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文