English 中文(简体)
Convert "current time" to "time in minutes since 00:00" calculation help
原标题:

I haven t used NSDate that much, so I d like some help with a calculation I am doing.

I have an integer that has my store s closing hour in minutes since midnight. Basically, my store closes at 5.00PM, which gives me the integer 1020.

So right now it s 10.45PM or 22.45. Integer since 00:00 is 1365, and from this I can say

if (storeHour < currentTime) {
      // closed!
}

However, I don t know how I get from "22.45" that I have from NSDate to converting it to an integer representing time since 00:00. Is there something in NSDate that can help me with that?

Thx

最佳回答

I do recommend using an NSDate object to store the time the store closes instead of using your own custom integer format. It s unfortunate that NSDate represents both date and time of date.

In any case, you can check NSDateComponents. You can have a utilities method like:

int minutesSinceMidnight:(NSDate *)date
{
    NSCalendar *gregorian = [[NSCalendar alloc]
        initWithCalendarIdentifier:NSGregorianCalendar];
    unsigned unitFlags =  NSHourCalendarUnit | NSMinuteCalendarUnit;
    NSDateComponents *components = [gregorian components:unitFlags fromDate:date];

    return 60 * [components hour] + [components minute];    
}
问题回答

Swift 5 This for exemple gives you the current number of minutes since midnight

let calendar = Calendar.current
let now = Date()

print(getMinutesSinceMidnight())

func getMinutesSinceMidnight() -> Int {
        return (calendar.component(.hour, from: now) * 60 + calendar.component(.minute, from: now))
 }

A swift version of what @notnoop has replied:

func minutesSinceMidnight(start : NSDate) -> Int {

    let units : NSCalendarUnit = [.Hour, .Minute]
    let components = NSCalendar.currentCalendar().components(units, fromDate: start)
    return 60 * components.hour + components.minute
}

If you have another NSDate which is set to midnight, then you can use the timeIntervalSinceDate method of the NSDate object to get an NSTimeInterval back with the difference between the two. Alternatively, if you re always wanting to compare midnight with the current time, you could call the timeIntervalSinceNow method on the midnight NSDate and you d get an NSTimeInterval back (albeit negative) with the difference between midnight and the current time.

NSTimeInterval is defined as a double by the standard which holds a number of seconds (and any fractional seconds).

This is a contemporary way without any extra math.

Objective-C

NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *midnight = [calendar startOfDayForDate:[NSDate date]];
NSInteger minutesSinceMidnight = [calendar components:NSCalendarUnitMinute fromDate:midnight toDate:[NSDate date] options:0].minute;

Swift 3+ as Date extension

extension Date {
    func minutesSinceMidnight() -> Int {
        let calendar = Calendar.current
        let midnight = calendar.startOfDay(for: self)
        return calendar.dateComponents([.minute], from: midnight, to: self).minute!
    }
}

Usage:

print(Date().minutesSinceMidnight())




相关问题
Code sign Error

I have created a new iPhone application.I have two mach machines. I have created the certificate for running application in iPhone in one mac. Can I use the other mac for running the application in ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

将音频Clips从Peter改为服务器

我不禁要问,那里是否有任何实例表明从Peit向服务器发送音响。 I m不关心电话或SIP风格的解决办法,只是一个简单的袖珍流程......

• 如何将搜查线重新定位?

我正试图把图像放在搜索条左边。 但是,问题始于这里,搜索条线不能重新布署。

热门标签