Iphone 将NSDate四舍五入到最接近的5分钟
比如我有Iphone 将NSDate四舍五入到最接近的5分钟,iphone,objective-c,cocoa,Iphone,Objective C,Cocoa,比如我有 NSDate *curDate = [NSDate date]; 它的值是上午9点13分。我没有使用curDate的年、月和日部分 我想得到的是时间值为9:15的日期;如果我的时间值是9:16,我想把它提前到9:20,以此类推 如何使用NSDate实现这一点?取分钟值,除以5四舍五入,得到下一个最高的5分钟单位,乘以5,将其恢复为分钟,然后构造一个新的NSDate NSDateComponents *time = [[NSCalendar currentCalendar]
NSDate *curDate = [NSDate date];
它的值是上午9点13分。我没有使用curDate的年、月和日部分
我想得到的是时间值为9:15的日期;如果我的时间值是9:16,我想把它提前到9:20,以此类推
如何使用NSDate实现这一点?取分钟值,除以5四舍五入,得到下一个最高的5分钟单位,乘以5,将其恢复为分钟,然后构造一个新的NSDate
NSDateComponents *time = [[NSCalendar currentCalendar]
components:NSHourCalendarUnit | NSMinuteCalendarUnit
fromDate:curDate];
NSInteger minutes = [time minute];
float minuteUnit = ceil((float) minutes / 5.0);
minutes = minuteUnit * 5.0;
[time setMinute: minutes];
curDate = [[NSCalendar currentCalendar] dateFromComponents:time];
我自己也一直在寻找这个,但使用上面的例子给出了0001年的日期 这是我的备选方案,结合了smorgan更优雅的mod建议,但请注意,我尚未对此进行泄漏测试:
NSDate *myDate = [NSDate date];
// Get the nearest 5 minute block
NSDateComponents *time = [[NSCalendar currentCalendar] components:NSHourCalendarUnit | NSMinuteCalendarUnit
fromDate:myDate];
NSInteger minutes = [time minute];
int remain = minutes % 5;
// Add the remainder of time to the date to round it up evenly
myDate = [myDate addTimeInterval:60*(5-remain)];
谢谢你的样品。
我在下面添加了一些代码,精确到5分钟
-(NSDate *)roundDateTo5Minutes:(NSDate *)mydate{
// Get the nearest 5 minute block
NSDateComponents *time = [[NSCalendar currentCalendar]
components:NSHourCalendarUnit | NSMinuteCalendarUnit
fromDate:mydate];
NSInteger minutes = [time minute];
int remain = minutes % 5;
// if less then 3 then round down
if (remain<3){
// Subtract the remainder of time to the date to round it down evenly
mydate = [mydate addTimeInterval:-60*(remain)];
}else{
// Add the remainder of time to the date to round it up evenly
mydate = [mydate addTimeInterval:60*(5-remain)];
}
return mydate;
}
-(NSDate*)rounddate到5分钟:(NSDate*)我的日期{
//去最近的5分钟街区
NSDateComponents*时间=[[NSCalendar currentCalendar]
组件:NSHourCalendarUnit | NSMinuteCalendarUnit
fromDate:mydate];
NSInteger分钟=[时间分钟];
int剩余=分钟%5;
//如果小于3,则向下取整
如果(保持以下是我使用ayianni的包装器思想解决原始问题(取整)的方法
-(NSDate *)roundDateToCeiling5Minutes:(NSDate *)mydate{
// Get the nearest 5 minute block
NSDateComponents *time = [[NSCalendar currentCalendar]
components:NSHourCalendarUnit | NSMinuteCalendarUnit
fromDate:mydate];
NSInteger minutes = [time minute];
int remain = minutes % 5;
// Add the remainder of time to the date to round it up evenly
mydate = [mydate addTimeInterval:60*(5-remain)];
return mydate;
}
我不确定NSDate组件的效率有多高,但如果您只想处理NSDate本身,它可以根据秒数为您提供值,然后可以对这些值进行操作
例如,此方法向下舍入到最接近的分钟。将60更改为300,它将向下舍入到最接近的5分钟
+ (NSDate *)dateRoundedDownToMinutes:(NSDate *)date {
// Strip miliseconds by converting to int
int referenceTimeInterval = (int)[date timeIntervalSinceReferenceDate];
int remainingSeconds = referenceTimeInterval % 60;
int timeRoundedDownToMinutes = referenceTimeInterval - remainingSeconds;
NSDate *roundedDownDate = [NSDate dateWithTimeIntervalSinceReferenceDate:(NSTimeInterval)timeRoundedDownToMinutes];
return roundedDownDate;
}
我认为这是最好的解决方案,但只是我的意见,基于之前的海报代码。四舍五入到最接近的5分钟标记。此代码使用的内存应该比日期组件解决方案少得多。太棒了,感谢您的指导
+(NSDate *) dateRoundedDownTo5Minutes:(NSDate *)dt{
int referenceTimeInterval = (int)[dt timeIntervalSinceReferenceDate];
int remainingSeconds = referenceTimeInterval % 300;
int timeRoundedTo5Minutes = referenceTimeInterval - remainingSeconds;
if(remainingSeconds>150)
{/// round up
timeRoundedTo5Minutes = referenceTimeInterval +(300-remainingSeconds);
}
NSDate *roundedDate = [NSDate dateWithTimeIntervalSinceReferenceDate:(NSTimeInterval)timeRoundedTo5Minutes];
return roundedDate;
}
以下是我的解决方案:
NSTimeInterval seconds = round([date timeIntervalSinceReferenceDate]/300.0)*300.0;
NSDate *rounded = [NSDate dateWithTimeIntervalSinceReferenceDate:seconds];
let original = Date()
let rounded = Date(timeIntervalSinceReferenceDate:
(original.timeIntervalSinceReferenceDate / 300.0).rounded(.toNearestOrEven) * 300.0)
我做了一些测试,它的速度大约是Voss的解决方案的十倍。一百万次迭代大约需要3.39秒。这一次只需要0.38秒。J3RM的解决方案需要0.50秒。内存使用率也应该是最低的
并不是说性能就是一切,而是一条直线。此外,您还可以通过除法和乘法轻松控制舍入
编辑:要回答此问题,您可以使用ceil
正确地汇总:
NSTimeInterval seconds = ceil([date timeIntervalSinceReferenceDate]/300.0)*300.0;
NSDate *rounded = [NSDate dateWithTimeIntervalSinceReferenceDate:seconds];
编辑:Swift中的扩展名:
public extension Date {
public func round(precision: TimeInterval) -> Date {
return round(precision: precision, rule: .toNearestOrAwayFromZero)
}
public func ceil(precision: TimeInterval) -> Date {
return round(precision: precision, rule: .up)
}
public func floor(precision: TimeInterval) -> Date {
return round(precision: precision, rule: .down)
}
private func round(precision: TimeInterval, rule: FloatingPointRoundingRule) -> Date {
let seconds = (self.timeIntervalSinceReferenceDate / precision).rounded(rule) * precision;
return Date(timeIntervalSinceReferenceDate: seconds)
}
}
不幸的是,这里的大多数回复都不完全正确(尽管它们对大多数用户来说似乎工作得很好),因为它们要么依赖当前的活动系统日历作为公历(可能不是这样)或者,由于闰秒不存在和/或总是被OS X和iOS忽略。以下代码用于复制和粘贴,保证是正确的,并且不会做出此类假设(因此,如果苹果更改闰秒支持,将来也不会中断,因为在这种情况下,NSCalendar也必须正确支持闰秒):
如果当前时间已经是5分钟的倍数,代码将不会更改它。原始问题没有明确说明这种情况。如果代码总是四舍五入到下一个5分钟的倍数,只需删除测试If(分钟%5){
而且它总是会结束。我刚刚开始为我的一个应用程序尝试这个,并提出了以下内容。它是用Swift编写的,但是这个概念应该足够容易理解,即使你不懂Swift
func skipToNextEvenFiveMinutesFromDate(date: NSDate) -> NSDate {
var componentMask : NSCalendarUnit = (NSCalendarUnit.CalendarUnitYear | NSCalendarUnit.CalendarUnitMonth | NSCalendarUnit.CalendarUnitDay | NSCalendarUnit.CalendarUnitHour | NSCalendarUnit.CalendarUnitMinute)
var components = NSCalendar.currentCalendar().components(componentMask, fromDate: date)
components.minute += 5 - components.minute % 5
components.second = 0
if (components.minute == 0) {
components.hour += 1
}
return NSCalendar.currentCalendar().dateFromComponents(components)!
}
结果在我的操场上看起来是正确的,在那里我输入了各种自定义日期,接近午夜,接近新年等等
编辑:Swift2支持:
func skipToNextEvenFiveMinutesFromDate(date: NSDate) -> NSDate {
let componentMask : NSCalendarUnit = ([NSCalendarUnit.Year , NSCalendarUnit.Month , NSCalendarUnit.Day , NSCalendarUnit.Hour ,NSCalendarUnit.Minute])
let components = NSCalendar.currentCalendar().components(componentMask, fromDate: date)
components.minute += 5 - components.minute % 5
components.second = 0
if (components.minute == 0) {
components.hour += 1
}
return NSCalendar.currentCalendar().dateFromComponents(components)!
}
我重写了@J3RM的解决方案,作为NSDate类中Swift的一个扩展。这里是将日期四舍五入到最接近的15分钟间隔:
extension NSDate
{
func nearestFifteenthMinute() -> NSDate!
{
let referenceTimeInterval = Int(self.timeIntervalSinceReferenceDate)
let remainingSeconds = referenceTimeInterval % 900
var timeRoundedTo5Minutes = referenceTimeInterval - remainingSeconds
if remainingSeconds > 450
{
timeRoundedTo5Minutes = referenceTimeInterval + (900 - remainingSeconds)
}
let roundedDate = NSDate.dateWithTimeIntervalSinceReferenceDate(NSTimeInterval(timeRoundedTo5Minutes))
return roundedDate
}
}
我知道这是一个较老的线程,但由于有更新的答案,我将分享我用于将NSDate四舍五入到最接近的5分钟间隔的实用方法
当UIDatePicker成为FirstResponder时,我使用此选项用当前UIDatePicker日期填充UIExtField。当UIDatePicker配置为1分钟间隔以外的时间间隔时,您不能只使用[NSDate date]。我的UIDatePicker配置为5分钟间隔
+ (NSDate *)roundToNearest5MinuteInterval {
NSDate *ceilingDate = [NSDate dateWithTimeIntervalSinceReferenceDate:ceil([[NSDate date] timeIntervalSinceReferenceDate]/300.0)*300.0];
NSDate *floorDate = [NSDate dateWithTimeIntervalSinceReferenceDate:floor([[NSDate date] timeIntervalSinceReferenceDate]/300.0)*300.0];
NSTimeInterval ceilingInterval = [ceilingDate timeIntervalSinceNow];
NSTimeInterval floorInterval = [floorDate timeIntervalSinceNow];
if (fabs(ceilingInterval) < fabs(floorInterval)) {
return ceilingDate;
} else {
return floorDate;
}
}
这是一个通用的解决方案,可四舍五入到最近的输入“分钟”:
+(NSDate *)roundUpDate:(NSDate *)aDate toNearestMins:(NSInteger)mins
{
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSUIntegerMax fromDate:aDate];
NSInteger dateMins = components.minute;
dateMins = ((dateMins+mins)/mins)*mins;
[components setMinute:dateMins];
[components setSecond:0];
return [[NSCalendar currentCalendar] dateFromComponents:components];
}
还有一个Swift通用解决方案,使用NSCalendar最多可工作30分钟
extension NSDate {
func nearest(minutes: Int) -> NSDate {
assert(minutes <= 30, "nearest(m) suppport rounding up to 30 minutes");
let cal = NSCalendar.currentCalendar();
var time = cal.components(.CalendarUnitMinute | .CalendarUnitSecond, fromDate: self);
let rem = time.minute % minutes
if rem > 0 {
time.minute = minutes - rem;
}
time.second = -time.second;
time.nanosecond = -time.nanosecond //updated 7.07.15
let date = cal.dateByAddingComponents(time, toDate: self, options: NSCalendarOptions(0));
return date!;
}
}
扩展日期{
func最近(分钟:Int)->NSDate{
断言(分钟0{
time.minute=分钟-rem;
}
time.second=-time.second;
time.nanosecond=-time.nanosecond//2015年7月7日更新
让date=cal.dateByAddingComponents(时间,toDate:self,选项:NSCalendarOptions(0));
返回日期!;
}
}
基于Chris和swift3的这个怎么样
import UIKit
enum DateRoundingType {
case round
case ceil
case floor
}
extension Date {
func rounded(minutes: TimeInterval, rounding: DateRoundingType = .round) -> Date {
return rounded(seconds: minutes * 60, rounding: rounding)
}
func rounded(seconds: TimeInterval, rounding: DateRoundingType = .round) -> Date {
var roundedInterval: TimeInterval = 0
switch rounding {
case .round:
roundedInterval = (timeIntervalSinceReferenceDate / seconds).rounded() * seconds
case .ceil:
roundedInterval = ceil(timeIntervalSinceReferenceDate / seconds) * seconds
case .floor:
roundedInterval = floor(timeIntervalSinceReferenceDate / seconds) * seconds
}
return Date(timeIntervalSinceReferenceDate: roundedInterval)
}
}
// Example
let nextFiveMinuteIntervalDate = Date().rounded(minutes: 5, rounding: .ceil)
print(nextFiveMinuteIntervalDate)
Wowsers,我在这里看到了很多答案,但很多答案都很长或很难理解,因此我将尝试投入2美分,以防有所帮助。NSCalendar
类以安全简洁的方式提供了所需的功能。这是一个适合我的解决方案,不需要乘以时间间隔秒、舍入或任何东西。NSCalendar
考虑闰日/年以及其他时间和日期异常。(Swift 2.2)
如果需要,它可以添加到NSDate
上的扩展中,或者作为一个自由形式的函数返回一个新的NSDate
实例,无论您需要什么。希望这能帮助任何需要它的人
Swift 3更新
let calendar = Calendar.current
let rightNow = Date()
let interval = 15
let nextDiff = interval - calendar.component(.minute, from: rightNow) % interval
let nextDate = calendar.date(byAdding: .minute, value: nextDiff, to: rightNow) ?? Date()
甚至更短…限制为秒:
let seconds = ceil(Date().timeIntervalSinceReferenceDate/300.0)*300.0
let roundedDate = Date(timeIntervalSinceReferenceDate: seconds)
有关苹果员工的完整详细答案,请参阅链接。要节省您的点击次数,请使用以下解决方案:
NSTimeInterval seconds = round([date timeIntervalSinceReferenceDate]/300.0)*300.0;
NSDate *rounded = [NSDate dateWithTimeIntervalSinceReferenceDate:seconds];
let original = Date()
let rounded = Date(timeIntervalSinceReferenceDate:
(original.timeIntervalSinceReferenceDate / 300.0).rounded(.toNearestOrEven) * 300.0)
在接下来的5分钟里,来自@ipje的消息起了作用,但我需要一些更灵活的方法,我想摆脱所有的魔法数字。
多亏了一个
我的解决方案使用Swift 5.2和测量
来避免使用幻数:
extension UnitDuration {
var upperUnit: Calendar.Component? {
if self == .nanoseconds {
return .second
}
if self == .seconds {
return .minute
}
if self == .minutes {
return .hour
}
if self == .hours {
return .day
}
return nil
}
}
extension Date {
func roundDate(to value: Int, in unit: UnitDuration, using rule: FloatingPointRoundingRule, and calendar: Calendar = Calendar.current) -> Date? {
guard unit != .picoseconds && unit != .nanoseconds,
let upperUnit = unit.upperUnit else { return nil }
let value = Double(value)
let unitMeasurement = Measurement(value: value, unit: unit)
let interval = unitMeasurement.converted(to: .seconds).value
let startOfPeriod = calendar.dateInterval(of: upperUnit, for: self)!.start
var seconds = self.timeIntervalSince(startOfPeriod)
seconds = (seconds / interval).rounded(rule) * interval
return startOfPeriod.addingTimeInterval(seconds)
}
func roundDate(toNearest value: Int, in unit: UnitDuration, using calendar: Calendar = Calendar.current) -> Date? {
return roundDate(to: value, in: unit, using: .toNearestOrEven)
}
func roundDate(toNext value: Int, in unit: UnitDuration, using calendar: Calendar = Calendar.current) -> Date? {
return roundDate(to: value, in: unit, using: .up)
}
}
在我的操场上:
let calendar = Calendar.current
let date = Calendar.current.date(from: DateComponents(timeZone: TimeZone.current, year: 2020, month: 6, day: 12, hour: 00, minute: 24, second: 17, nanosecond: 577881))! // 12 Jun 2020 at 00:24
var roundedDate = date.roundDate(toNext: 5, in: .seconds)!
//"12 Jun 2020 at 00:24"
calendar.dateComponents([.nanosecond, .second, .minute, .hour, .day, .month], from: roundedDate)
// month: 6 day: 12 hour: 0 minute: 24 second: 20 nanosecond: 0 isLeapMonth: false
roundedDate = date.roundDate(toNearest: 5, in: .seconds)!
// "12 Jun 2020 at 00:24"
calendar.dateComponents([.nanosecond, .second, .minute, .hour, .day, .month], from: roundedDate)
// month: 6 day: 12 hour: 0 minute: 24 second: 15 nanosecond: 0 isLeapMonth: false
roundedDate = date.roundDate(toNext: 5, in: .minutes)!
// "12 Jun 2020 at 00:25"
calendar.dateComponents([.nanosecond, .second, .minute, .hour, .day, .month], from: roundedDate)
// month: 6 day: 12 hour: 0 minute: 25 second: 0 nanosecond: 0 isLeapMonth: false
roundedDate = date.roundDate(toNearest: 5, in: .minutes)!
// "12 Jun 2020 at 00:25"
calendar.dateComponents([.nanosecond, .second, .minute, .hour, .day, .month], from: roundedDate)
// month: 6 day: 12 hour: 0 minute: 25 second: 0 nanosecond: 0 isLeapMonth: false
roundedDate = date.roundDate(toNext: 5, in: .hours)!
// "12 Jun 2020 at 05:00"
calendar.dateComponents([.nanosecond, .second, .minute, .hour, .day, .month], from: roundedDate)
// month: 6 day: 12 hour: 5 minute: 0 second: 0 nanosecond: 0 isLeapMonth: false
roundedDate = date.roundDate(toNearest: 5, in: .hours)!
// "12 Jun 2020 at 00:00"
calendar.dateComponents([.nanosecond, .second, .minute, .hour, .day, .month], from: roundedDate)
// month: 6 day: 12 hour: 0 minute: 0 second: 0 nanosecond: 0 isLeapMonth: false
另一种选择
extension UnitDuration {
var upperUnit: Calendar.Component? {
if self == .nanoseconds {
return .second
}
if self == .seconds {
return .minute
}
if self == .minutes {
return .hour
}
if self == .hours {
return .day
}
return nil
}
}
extension Date {
func roundDate(to value: Int, in unit: UnitDuration, using rule: FloatingPointRoundingRule, and calendar: Calendar = Calendar.current) -> Date? {
guard unit != .picoseconds && unit != .nanoseconds,
let upperUnit = unit.upperUnit else { return nil }
let value = Double(value)
let unitMeasurement = Measurement(value: value, unit: unit)
let interval = unitMeasurement.converted(to: .seconds).value
let startOfPeriod = calendar.dateInterval(of: upperUnit, for: self)!.start
var seconds = self.timeIntervalSince(startOfPeriod)
seconds = (seconds / interval).rounded(rule) * interval
return startOfPeriod.addingTimeInterval(seconds)
}
func roundDate(toNearest value: Int, in unit: UnitDuration, using calendar: Calendar = Calendar.current) -> Date? {
return roundDate(to: value, in: unit, using: .toNearestOrEven)
}
func roundDate(toNext value: Int, in unit: UnitDuration, using calendar: Calendar = Calendar.current) -> Date? {
return roundDate(to: value, in: unit, using: .up)
}
}
let calendar = Calendar.current
let date = Calendar.current.date(from: DateComponents(timeZone: TimeZone.current, year: 2020, month: 6, day: 12, hour: 00, minute: 24, second: 17, nanosecond: 577881))! // 12 Jun 2020 at 00:24
var roundedDate = date.roundDate(toNext: 5, in: .seconds)!
//"12 Jun 2020 at 00:24"
calendar.dateComponents([.nanosecond, .second, .minute, .hour, .day, .month], from: roundedDate)
// month: 6 day: 12 hour: 0 minute: 24 second: 20 nanosecond: 0 isLeapMonth: false
roundedDate = date.roundDate(toNearest: 5, in: .seconds)!
// "12 Jun 2020 at 00:24"
calendar.dateComponents([.nanosecond, .second, .minute, .hour, .day, .month], from: roundedDate)
// month: 6 day: 12 hour: 0 minute: 24 second: 15 nanosecond: 0 isLeapMonth: false
roundedDate = date.roundDate(toNext: 5, in: .minutes)!
// "12 Jun 2020 at 00:25"
calendar.dateComponents([.nanosecond, .second, .minute, .hour, .day, .month], from: roundedDate)
// month: 6 day: 12 hour: 0 minute: 25 second: 0 nanosecond: 0 isLeapMonth: false
roundedDate = date.roundDate(toNearest: 5, in: .minutes)!
// "12 Jun 2020 at 00:25"
calendar.dateComponents([.nanosecond, .second, .minute, .hour, .day, .month], from: roundedDate)
// month: 6 day: 12 hour: 0 minute: 25 second: 0 nanosecond: 0 isLeapMonth: false
roundedDate = date.roundDate(toNext: 5, in: .hours)!
// "12 Jun 2020 at 05:00"
calendar.dateComponents([.nanosecond, .second, .minute, .hour, .day, .month], from: roundedDate)
// month: 6 day: 12 hour: 5 minute: 0 second: 0 nanosecond: 0 isLeapMonth: false
roundedDate = date.roundDate(toNearest: 5, in: .hours)!
// "12 Jun 2020 at 00:00"
calendar.dateComponents([.nanosecond, .second, .minute, .hour, .day, .month], from: roundedDate)
// month: 6 day: 12 hour: 0 minute: 0 second: 0 nanosecond: 0 isLeapMonth: false