说时间字符串值是"7:00 AM“,称之为提醒时间。
现在,我需要做的就是比较这个时间和当前的日期时间,如果提醒时间比当前时间晚的话,就说它的"9:00 AM“--返回true AM false。这是日期格式化器的格式"h:mm a“。
很简单对吧?应该是的,但我在这件事上花了太多时间。我可以得到小时和分钟的值,但是当AM/PM被考虑的时候,它变得更难了。
我只想比较两个时间值,并确定第一个是晚一点还是在第二个之后。日期总是今天或当前日期,所以我只关心日期的时间部分。当然,您必须转换为日期来进行比较,但是当前日期很容易得到,但是从"7:00 AM“字符串中得到的日期在比较中似乎不正确。
有人有这个功能吗?
谢谢。
发布于 2017-04-04 03:53:42
方法是让您从当前的time对象中对Date()对象进行日期化,这样您就可以得到
默认日期+时间=
2000-01-01 00:00:00+your time (7.00 AM or 9.00 PM)
现在我们将只从今天开始,以同样的格式获得当前时间。(只有时间)
它将类似于
3.56 PM
现在,我们将再次将此3.56 PM转换为Date(),默认日期为prev。现在我们将有两个日期时间对象,具有相同的日期(2000-01-01)和相应的时间。
现在我们将比较两个日期对象。
检查小提琴 小提琴
func CompareMyTimeInString(myTime:String)->Bool
{
// create the formatter - we are expecting only "hh:mm a" format
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "hh:mm a"
dateFormatter.locale = Locale.init(identifier: "en_GB")
// default date with my time
var dt_MyTime = dateFormatter.date(from: yourTime)!
// current time in same format as string "hh:mm a"
var currentTimString = dateFormatter.string(from: Date());
print("Current Time is - "+currentTimString);
// current time with default date.
var dt_CurrentTime = dateFormatter.date(from: currentTimString)!
// now just compare two date objects :)
return dt_MyTime > dt_CurrentTime;
}
// then call it like
var yourTime = "7.00 AM"
var isDue = CompareMyTimeInString(myTime:yourTime);
print(isDue);发布于 2017-04-04 14:18:50
我的解决办法如下。
私有函数ReminderAfterCurrentTime( reminderTimeString : String) -> Bool {//比较两个时间字符串,如果reminderTimeString比当前时间字符串//返回真,因为提醒是在当前时间之后。
//Get the current date and time
let currentDateTime = Date()
// Create calendar object
let calendar = NSCalendar.current
// Get current date hour and minute values for comparison.
let currentHourValue = Int(calendar.component(.hour, from: currentDateTime))
let currentMinuteValue = Int(calendar.component(.minute, from: currentDateTime))
//Now get a date from the time string passed in so we can get just the hours and minutes to compare
let dateformatter = DateFormatter()
dateformatter.dateStyle = DateFormatter.Style.none
dateformatter.timeStyle = DateFormatter.Style.short
//Now get the date using formatter.
let reminderDateTime = dateformatter.date(from: reminderTimeString)
print("reminderDateTime = \(reminderDateTime)")
//Get reminder hour and minute for comparison.
let reminderHourValue = Int(calendar.component(.hour, from: reminderDateTime!))
let reminderMinuteValue = Int(calendar.component(.minute, from: reminderDateTime!))
print("currentHourValue = \(currentHourValue)")
print("currentMinuteValue = \(currentMinuteValue)")
print("reminderHourValue = \(reminderHourValue)")
print("reminderMinuteValue = \(reminderMinuteValue)")
//This works due to 24 hour clock. Thus AM/PM is already taken into account.
if currentHourValue < reminderHourValue {
return true
}
//Check for same hour then use minutes
if currentHourValue == reminderHourValue {
if currentMinuteValue < reminderMinuteValue {
return true
}
}
//Otherwise return false
return false}
https://stackoverflow.com/questions/43190946
复制相似问题