gpt4 book ai didi

ios - NSDate - 返回最近的小时

转载 作者:行者123 更新时间:2023-11-30 13:48:48 25 4
gpt4 key购买 nike

我想创建一个项目:

  1. 我创建了一个要执行的任务。
  2. 我为此任务创建一个停止日期
  3. 我使用日期选择器创建了三个提醒

这我已经完成了。但现在,我希望标签为我返回最近提醒的时间(但我希望标签在停止日期之前执行此操作)

我已经完成的是一个 Playground 项目:

let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm"
let currentDate = dateFormatter.dateFromString("2016-01-03 16:30")

var firstDate = dateFormatter.dateFromString("2016-01-03 13:00")
var secondDate = dateFormatter.dateFromString("2016-01-03 16:00")
var thirdDate = dateFormatter.dateFromString("2016-01-03 17:00")
var datesArray = [firstDate, secondDate, thirdDate]


let firstInterval = firstDate?.timeIntervalSinceDate(currentDate!)
let secondInterval = secondDate?.timeIntervalSinceDate(currentDate!)
let thirdInterval = thirdDate?.timeIntervalSinceDate(currentDate!)
let intervalArray = [firstInterval, secondInterval, thirdInterval]


var aboveZeroIntervals = [NSTimeInterval]()
for interval in intervalArray {
if interval > 0 {
aboveZeroIntervals.append(interval!)
}
}
//print(aboveZeroIntervals)

for date in datesArray {
if date?.timeIntervalSinceDate(currentDate!) == aboveZeroIntervals.minElement() {
print(dateFormatter.stringFromDate(date!))
}
}

如何仅获取第一个/第二个/第三个日期中的小时并显示这三个日期中最接近 currentDate 小时的小时?并在currentDate超过stopDoingDate后完美完成显示...

我想出了这样的东西(代码如下)。谁能看一下并告诉我是否有某种方法可以简化这个?

extension NSDate
{
func isGreaterThanDate(dateToCompare : NSDate) -> Bool
{
//Declare Variables
var isGreater = false

//Compare Values
if self.compare(dateToCompare) == NSComparisonResult.OrderedDescending
{
isGreater = true
}

//Return Result
return isGreater
}
}

let calendar = NSCalendar.currentCalendar()
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm"
let currentDate = dateFormatter.dateFromString("2016-01-04 16:30")
let stopDoingDate = dateFormatter.dateFromString("2016-01-06 15:00")


if stopDoingDate!.isGreaterThanDate(currentDate!) {
let currentDateComponents = calendar.components([.Year, .Month, .Day, .Hour, .Minute], fromDate: currentDate!)
currentDateComponents.hour
currentDateComponents.minute
let dateTimeFormatter = NSDateFormatter()
dateTimeFormatter.dateFormat = "HH:mm"
let currentOnlyTime = dateTimeFormatter.dateFromString("\(currentDateComponents.hour):\(currentDateComponents.minute)")

var firstDate = dateFormatter.dateFromString("2016-01-03 13:00")
var firstDateComponents = calendar.components([.Hour, .Minute], fromDate: firstDate!)
var firstDateTime = dateTimeFormatter.dateFromString("\(firstDateComponents.hour):\(firstDateComponents.minute)")

var secondDate = dateFormatter.dateFromString("2016-01-03 16:00")
var secondDateComponents = calendar.components([.Hour, .Minute], fromDate: secondDate!)
var secondDateTime = dateTimeFormatter.dateFromString("\(secondDateComponents.hour):\(secondDateComponents.minute)")

var thirdDate = dateFormatter.dateFromString("2016-01-03 17:00")
var thirdDateComponents = calendar.components([.Hour, .Minute], fromDate: thirdDate!)
var thirdDateTime = dateTimeFormatter.dateFromString("\(thirdDateComponents.hour):\(thirdDateComponents.minute)")

var datesArray = [firstDateTime, secondDateTime, thirdDateTime]


let firstInterval = firstDateTime?.timeIntervalSinceDate(currentOnlyTime!)
let secondInterval = secondDateTime?.timeIntervalSinceDate(currentOnlyTime!)
let thirdInterval = thirdDateTime?.timeIntervalSinceDate(currentOnlyTime!)
let intervalArray = [firstInterval, secondInterval, thirdInterval]


var aboveZeroIntervals = [NSTimeInterval]()
for interval in intervalArray {
if interval > 0 {
aboveZeroIntervals.append(interval!)
}
}
//print(aboveZeroIntervals)
for date in datesArray {
if date?.timeIntervalSinceDate(currentOnlyTime!) == aboveZeroIntervals.minElement() {
let dateTime = calendar.components([.Hour, .Minute], fromDate: date!)

print(dateFormatter.dateFromString("\(currentDateComponents.year)-\(currentDateComponents.month)-\(currentDateComponents.day) \(dateTime.hour):\(dateTime.minute)")!)
print("Next todo hour is: \(dateTime.hour): \(dateTime.minute)")

}
}
} else {
print("Todo DONE")
}

最佳答案

我想出了这个:唯一的问题是生成从 currentDate 到 stopDoingDate 的日期数组,但我将单独处理这个问题(并且可能也会在此处发布解决方案)

我使用此论坛帖子之一来生成 NSDate 扩展:

extension NSDate
{
func isGreaterThanDate(dateToCompare : NSDate) -> Bool
{
//Declare Variables
var isGreater = false

//Compare Values
if self.compare(dateToCompare) == NSComparisonResult.OrderedDescending
{
isGreater = true
}

//Return Result
return isGreater
}
}

//let calendar = NSCalendar.currentCalendar()
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm"
let currentDate = dateFormatter.dateFromString("2016-01-04 14:00")!
let stopDoingDate = dateFormatter.dateFromString("2016-01-06 16:00")!
let date1 = dateFormatter.dateFromString("2016-01-04 09:00")
let date2 = dateFormatter.dateFromString("2016-01-04 15:00")
let date3 = dateFormatter.dateFromString("2016-01-05 09:00")
let date4 = dateFormatter.dateFromString("2016-01-05 15:00")
let date5 = dateFormatter.dateFromString("2016-01-06 09:00")
let date6 = dateFormatter.dateFromString("2016-01-06 15:00")
var dates: [NSDate] = [date1!, date2!, date3!, date6!, date5!, date4!]

if stopDoingDate.isGreaterThanDate(currentDate) {
var datesAfterCurrentDate = [NSDate]()
for date in dates {
if date.isGreaterThanDate(currentDate) {
datesAfterCurrentDate.append(date)
}
}
let sortedDatesAfterCurrentDate = datesAfterCurrentDate.sort({ $0.timeIntervalSince1970 < $1.timeIntervalSince1970 })
let dateToDisplay = sortedDatesAfterCurrentDate.first!
let timeFormatter = NSDateFormatter()
timeFormatter.timeStyle = .ShortStyle
print(timeFormatter.stringFromDate(dateToDisplay)) } else {
print("We are done here")
}

这个论坛真是太棒了!感谢 Leo Dabus 的简单想法!

关于ios - NSDate - 返回最近的小时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34582836/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com