将“当前时间”转换为“当前时间”至“自00:00起以分钟为单位的时间”计算帮助

发布于 2024-08-13 07:04:35 字数 343 浏览 3 评论 0原文

我没有经常使用 NSDate,所以我需要一些帮助来完成我正在做的计算。

我有一个整数,其中包含自午夜以来我的商店的关闭时间(以分钟为单位)。基本上,我的商店在下午 5 点关门,这给了我整数 1020。

所以现在是晚上 10.45 或 22.45。从 00:00 开始的整数是 1365,由此我可以说

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

但是,我不知道如何从 NSDate 获得的“22.45”将其转换为表示从 00:00 开始的时间的整数。 NSDate 中有什么可以帮助我的吗?

谢谢

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

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

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

发布评论

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

评论(5

以往的大感动 2024-08-20 07:04:35

我确实建议使用 NSDate 对象来存储商店关闭的时间,而不是使用您自己的自定义整数格式。不幸的是,NSDate 既代表日期又代表日期时间。

无论如何,您可以检查 NSDateComponents。您可以有一个实用程序方法,例如:

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];    
}

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];    
}
我也只是我 2024-08-20 07:04:35

斯威夫特5
例如,这为您提供了自午夜以来的当前分钟数

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

print(getMinutesSinceMidnight())

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

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))
 }
倒数 2024-08-20 07:04:35

@notnoop 回复的快速版本:

func minutesSinceMidnight(start : NSDate) -> Int {

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

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
}
弱骨蛰伏 2024-08-20 07:04:35

如果您有另一个设置为午夜的NSDate,那么您可以使用NSDate对象的timeIntervalSinceDate方法来获取NSTimeInterval两者之间的区别。或者,如果您总是想将午夜与当前时间进行比较,您可以在午夜 NSDate 上调用 timeIntervalSinceNow 方法,并且您会得到一个 NSTimeInterval 返回(尽管负)与午夜和当前时间之间的差异。

NSTimeInterval 由标准定义为 double,它保存秒数(以及任何小数秒)。

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).

妄司 2024-08-20 07:04:35

这是一种现代方法,不需要任何额外的数学。

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+ 作为 Date 扩展

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

用法:

print(Date().minutesSinceMidnight())

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())
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文