NSDate 将秒数清零而不向上舍入

发布于 2024-10-23 13:32:50 字数 487 浏览 1 评论 0原文

我想知道是否有人可以帮助我用我的方法。我有以下方法,它将 NSDate 对象的秒值归零:

- (NSDate *)dateWithZeroSeconds:(NSDate *)date {
    NSTimeInterval time = round([date timeIntervalSinceReferenceDate] / 60.0) * 60.0;
    return  [NSDate dateWithTimeIntervalSinceReferenceDate:time];
}

问题是当传递一个日期时,例如:

2011-03-16 18:21:43 +0000

它返回:

2011-03-16 18:22:00 +0000

我不希望发生这种舍入,因为实际上是用户指定日期,因此需要精确到他们要求的分钟。

非常感谢任何帮助。

I would like to know if anyone can help me with my method. I have the following method, which will zero out the seconds value of a NSDate object:

- (NSDate *)dateWithZeroSeconds:(NSDate *)date {
    NSTimeInterval time = round([date timeIntervalSinceReferenceDate] / 60.0) * 60.0;
    return  [NSDate dateWithTimeIntervalSinceReferenceDate:time];
}

The problem is when passed a date such as:

2011-03-16 18:21:43 +0000

it returns:

2011-03-16 18:22:00 +0000

I do not want this rounding to occur, as it is a user who is actually specifying the date, so it needs to be exact to the minute they request.

Any help is greatly appreciated.

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

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

发布评论

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

评论(6

江城子 2024-10-30 13:32:50

使用地板代替圆形:

- (NSDate *)dateWithZeroSeconds:(NSDate *)date
{
    NSTimeInterval time = floor([date timeIntervalSinceReferenceDate] / 60.0) * 60.0;
    return  [NSDate dateWithTimeIntervalSinceReferenceDate:time];
}

Use floor instead of round:

- (NSDate *)dateWithZeroSeconds:(NSDate *)date
{
    NSTimeInterval time = floor([date timeIntervalSinceReferenceDate] / 60.0) * 60.0;
    return  [NSDate dateWithTimeIntervalSinceReferenceDate:time];
}
漫漫岁月 2024-10-30 13:32:50

使用 NSCalendar 和 NSDateComponents 获取日期的各个部分。将秒组件设置为 0,然后从该 NSDateComponents 创建一个新日期。

Use NSCalendar and NSDateComponents to get the parts of the date. Set the seconds component to 0, then create a new date from that NSDateComponents.

长亭外,古道边 2024-10-30 13:32:50

为了完整起见,这里是使用 NSCalendar 和 NSDateComponents 引用 iOS SDK 8.1 的代码。

+ (NSDate *)truncateSecondsForDate:(NSDate *)fromDate;
{
     NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
     NSCalendarUnit unitFlags = NSCalendarUnitEra | NSCalendarUnitYear | NSCalendarUnitMonth |  NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute;
     NSDateComponents *fromDateComponents = [gregorian components:unitFlags fromDate:fromDate ];
     return [gregorian dateFromComponents:fromDateComponents];
}

请注意,从 iOS 8 开始,日历单位名称已更改。

To be complete, here is the code referenced to iOS SDK 8.1 using NSCalendar and NSDateComponents.

+ (NSDate *)truncateSecondsForDate:(NSDate *)fromDate;
{
     NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
     NSCalendarUnit unitFlags = NSCalendarUnitEra | NSCalendarUnitYear | NSCalendarUnitMonth |  NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute;
     NSDateComponents *fromDateComponents = [gregorian components:unitFlags fromDate:fromDate ];
     return [gregorian dateFromComponents:fromDateComponents];
}

Note that as of iOS 8 the calendar unit names have changed.

心不设防 2024-10-30 13:32:50

您可以使用 rangeOfUnit:startDate:interval:forDate: 获取任何时间单位(例如一分钟)的开始时间

NSDate *startOfMinuteDate;
[[NSCalendar currentCalendar] rangeOfUnit:NSCalendarUnitMinute
                                startDate:&startOfMinuteDate 
                                 interval:NULL 
                                  forDate:originalDate];

You can get the start of any time unit — such as an minute — with rangeOfUnit:startDate:interval:forDate:

NSDate *startOfMinuteDate;
[[NSCalendar currentCalendar] rangeOfUnit:NSCalendarUnitMinute
                                startDate:&startOfMinuteDate 
                                 interval:NULL 
                                  forDate:originalDate];
闻呓 2024-10-30 13:32:50

Swift 2.2 版本的@Neil 回答:

func truncateSecondsForDate(fromDate: NSDate) -> NSDate {

    let calendar : NSCalendar = NSCalendar.currentCalendar()
    let unitFlags : NSCalendarUnit = [.Era , .Year , .Month , .Day , .Hour , .Minute]
    let fromDateComponents: NSDateComponents = calendar.components(unitFlags, fromDate: fromDate)

    return calendar.dateFromComponents(fromDateComponents)!

}

Swift 2.2 version of @Neil answer:

func truncateSecondsForDate(fromDate: NSDate) -> NSDate {

    let calendar : NSCalendar = NSCalendar.currentCalendar()
    let unitFlags : NSCalendarUnit = [.Era , .Year , .Month , .Day , .Hour , .Minute]
    let fromDateComponents: NSDateComponents = calendar.components(unitFlags, fromDate: fromDate)

    return calendar.dateFromComponents(fromDateComponents)!

}
一袭白衣梦中忆 2024-10-30 13:32:50

斯威夫特 4 版本:

func truncateSecondsForDate(fromDate: Date) -> Date {

    let calendar = Calendar.current
    let fromDateComponents: DateComponents = calendar.dateComponents([.era , .year , .month , .day , .hour , .minute], from: fromDate as Date) as DateComponents

    return calendar.date(from: fromDateComponents as DateComponents)! as Date
}

Swift 4 version :

func truncateSecondsForDate(fromDate: Date) -> Date {

    let calendar = Calendar.current
    let fromDateComponents: DateComponents = calendar.dateComponents([.era , .year , .month , .day , .hour , .minute], from: fromDate as Date) as DateComponents

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