UIDatePicker 间隔 15m,但始终作为返回值的精确时间

发布于 2024-12-05 16:01:53 字数 360 浏览 0 评论 0原文

我有一个时间模式下的 UIDatePicker,间隔为 15 分钟。

假设现在是晚上 7:22。日期选择器默认显示的值是四舍五入到下一个较高/较低值的当前时间,在本例中为晚上 7:15。如果用户与日期选择器交互,则显示的值也是返回的值。但是,如果没有任何用户交互,并且我使用 [UIDatePicker date] 获取时间,则返回值是准确的当前时间,即 7:22(继续使用示例)。

即使用户没有明确选择一个值,是否有任何方法可以始终获取日期选择器上实际显示的值而不是当前的未舍入时间?

我已经尝试使用 [UIDatePicker setDate:[NSDate date]] 手动设置选择器,但这不会改变行为。

I've got a UIDatePicker in Time mode with an interval of 15 minutes.

Let's say it's 7:22PM. The value that the date picker shows per default is the current time rounded to the next higher/lower value, in this case, it's 7:15PM. If the user interacts with the date picker, the value that's shown is also the value returned. However if there hasn't been any user interaction and I get the time with [UIDatePicker date] the return value is the exact current time, i.e. 7:22 to stick with the example.

Is there any way to always get the value that's actually shown on the date picker instead of the current unrounded time, even if the user hasn't explicitly picked a value?

I've already tried to set the picker manually with [UIDatePicker setDate:[NSDate date]] but that doesn't change the behavior.

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

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

发布评论

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

评论(3

故事与诗 2024-12-12 16:01:54

@ima747 是正确的,如果我更改了日期选择器的默认值,那么它工作正常,如果我在没有 onChange 的情况下从选择器控件中读取了日期,那么它会返回错误的日期值。

但我已经检查过,日期选择器控件已设置默认日期(当前日期,可能使用最近的时间毫秒)如果我将其设置为自定义日期并将时间设置为任何日期和 1:00:AM,那么现在我的时间选择器总是以 1:00:AM 而不是当前时间打开
输入图片此处描述

@ima747 is right if I've changed date picker's default value then it's working fine and if I've read date from picker control without onChange then it's returning wrong date value.

But I've checked that, date picker control have set default date (current date and may be it use nearest time milliseconds) If I set it to custom date and also set time to any date and 1:00:AM so now my time picker always open with 1:00:AM instead of current time
enter image description here

油焖大侠 2024-12-12 16:01:53

我已经修改了 @ima747 的 Swift 3/4 答案并作为 UIDatePicker 的扩展。 picker.clampedDate

extension UIDatePicker {
    /// Returns the date that reflects the displayed date clamped to the `minuteInterval` of the picker.
    /// - note: Adapted from [ima747's](http://stackoverflow.com/users/463183/ima747) answer on [Stack Overflow](http://stackoverflow.com/questions/7504060/uidatepicker-with-15m-interval-but-always-exact-time-as-return-value/42263214#42263214})
    public var clampedDate: Date {
        let referenceTimeInterval = self.date.timeIntervalSinceReferenceDate
        let remainingSeconds = referenceTimeInterval.truncatingRemainder(dividingBy: TimeInterval(minuteInterval*60))
        let timeRoundedToInterval = referenceTimeInterval - remainingSeconds
        return Date(timeIntervalSinceReferenceDate: timeRoundedToInterval)
    }
}

I've modified @ima747 's answer for Swift 3/4 and as an extension of UIDatePicker. picker.clampedDate

extension UIDatePicker {
    /// Returns the date that reflects the displayed date clamped to the `minuteInterval` of the picker.
    /// - note: Adapted from [ima747's](http://stackoverflow.com/users/463183/ima747) answer on [Stack Overflow](http://stackoverflow.com/questions/7504060/uidatepicker-with-15m-interval-but-always-exact-time-as-return-value/42263214#42263214})
    public var clampedDate: Date {
        let referenceTimeInterval = self.date.timeIntervalSinceReferenceDate
        let remainingSeconds = referenceTimeInterval.truncatingRemainder(dividingBy: TimeInterval(minuteInterval*60))
        let timeRoundedToInterval = referenceTimeInterval - remainingSeconds
        return Date(timeIntervalSinceReferenceDate: timeRoundedToInterval)
    }
}
凉栀 2024-12-12 16:01:53

这不是一个完美的解决方案,但它对我有用。我从另一篇文章中获取了它并对其进行了修改以接受不同的值。

- (NSDate*)clampDate:(NSDate *)dt toMinutes:(int)minutes {
    int referenceTimeInterval = (int)[dt timeIntervalSinceReferenceDate];
    int remainingSeconds = referenceTimeInterval % (minutes*60);
    int timeRoundedTo5Minutes = referenceTimeInterval - remainingSeconds; 
    if(remainingSeconds>((minutes*60)/2)) {/// round up
        timeRoundedTo5Minutes = referenceTimeInterval +((minutes*60)-remainingSeconds);            
    }
    return [NSDate dateWithTimeIntervalSinceReferenceDate:(NSTimeInterval)timeRoundedTo5Minutes];
}

向其提供输入日期和分钟值(在此线程的情况下取自 UIDatePicker.monthInterval),它将返回一个固定在该间隔的时间,您可以将其提供给选择器。

This isn't a perfect solution but it works for me. I've taken it from another post and modified it to accept different values.

- (NSDate*)clampDate:(NSDate *)dt toMinutes:(int)minutes {
    int referenceTimeInterval = (int)[dt timeIntervalSinceReferenceDate];
    int remainingSeconds = referenceTimeInterval % (minutes*60);
    int timeRoundedTo5Minutes = referenceTimeInterval - remainingSeconds; 
    if(remainingSeconds>((minutes*60)/2)) {/// round up
        timeRoundedTo5Minutes = referenceTimeInterval +((minutes*60)-remainingSeconds);            
    }
    return [NSDate dateWithTimeIntervalSinceReferenceDate:(NSTimeInterval)timeRoundedTo5Minutes];
}

Feed it an input date and a minute value (taken from the UIDatePicker.minuteInterval in the case of this thread) and it will return a time clamped to that interval which you can feed to the picker.

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