Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Date 需要一种简单的方法将时间字符串与当前日期时间值进行比较_Date_Time_Swift3_Comparison - Fatal编程技术网

Date 需要一种简单的方法将时间字符串与当前日期时间值进行比较

Date 需要一种简单的方法将时间字符串与当前日期时间值进行比较,date,time,swift3,comparison,Date,Time,Swift3,Comparison,假设时间字符串值为“7:00AM”,则称之为提醒时间 现在,我需要做的就是将这个时间与当前日期时间进行比较,如果提醒时间晚于当前时间,请说它是“上午9:00”——返回true,否则返回false。这是日期格式化程序的格式“h:mm a” 简单对吧?应该是的,但我在这件事上浪费了太多时间。我可以得到小时和分钟的值,但当考虑到AM/PM时,它会变得更难 我只想比较两个时间值,确定第一个时间值是晚于第二个时间值还是晚于第二个时间值。日期始终是今天或当前日期,因此我只关心日期的时间部分。当然,您必须转换

假设时间字符串值为“7:00AM”,则称之为提醒时间

现在,我需要做的就是将这个时间与当前日期时间进行比较,如果提醒时间晚于当前时间,请说它是“上午9:00”——返回true,否则返回false。这是日期格式化程序的格式“h:mm a”

简单对吧?应该是的,但我在这件事上浪费了太多时间。我可以得到小时和分钟的值,但当考虑到AM/PM时,它会变得更难

我只想比较两个时间值,确定第一个时间值是晚于第二个时间值还是晚于第二个时间值。日期始终是今天或当前日期,因此我只关心日期的时间部分。当然,您必须转换为日期来进行比较,但是当前日期很容易获取,但是“7:00am”字符串的日期在比较中似乎不起作用

有人能做到这一点吗


谢谢。

方法是让我们从当前时间对象中指定
date()
对象的日期,这样您就可以

默认日期+您的时间=
2000-01-01 00:00:00
+
您的时间(上午7点或晚上9点)

现在我们将以相同的格式获取从今天开始的当前时间。(仅限时间)

大概是下午3点56分

现在我们再次将此
3.56 PM
转换为
Date()
,默认日期为prev。现在我们将有两个日期时间对象,它们具有相同的日期(2000-01-01)和各自的时间

  • 2000-01-01 7:00:00=>这将显示您的默认日期为上午7:00
  • 2000-01-01 15:56:00=>这将是带有默认日期的当前时间
现在我们将比较两个日期对象

检查小提琴


我的解决办法如下

private func ReminderAfterCurrentTime(ReminderItemString:String)->Bool{ //比较这两个时间字符串,并确定RememberTimeString是否晚于当前时间字符串 //返回true,因为提醒在当前时间之后

//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
//获取当前日期和时间
让currentDateTime=Date()
//创建日历对象
让日历=NSCalendar.current
//获取当前日期小时和分钟值以进行比较。
让currentHourValue=Int(calendar.component(.hour,from:currentDateTime))
让currentMinuteValue=Int(calendar.component(.minute,from:currentDateTime))
//现在从传入的时间字符串中获取一个日期,这样我们就可以只获取要比较的小时和分钟
让dateformatter=dateformatter()
dateformatter.dateStyle=dateformatter.Style.none
dateformatter.timeStyle=dateformatter.Style.short
//现在使用格式化程序获取日期。
let-reminderDateTime=dateformatter.date(from:reminderTimeString)
打印(“提醒日期时间=\(提醒日期时间)”)
//获取用于比较的提醒时间和分钟。
让reminderHourValue=Int(calendar.component(.hour,from:reminderDateTime!))
让reminderMinuteValue=Int(calendar.component(.minute,from:reminderDateTime!))
打印(“currentHourValue=\(currentHourValue)”)
打印(“currentMinuteValue=\(currentMinuteValue)”)
打印(“提醒HourValue=\(提醒HourValue)”)
打印(“reminderMinuteValue=\(reminderMinuteValue)”)
//这是因为24小时制。因此,AM/PM已被考虑在内。
如果当前小时值<提醒小时值{
返回真值
}
//检查同一小时,然后使用分钟
如果currentHourValue==提醒HourValue{
如果currentMinuteValue

}

首先将AM/PM作为第一部分,然后添加“AM 7.00”或“PM 9.00”之类的时间,然后通常将其作为字符串进行比较。诀窍是我们的时间因子先大后小。所以现在正常的比较大一点小一点会起作用。AM PM表示12小时,然后下一个数字表示1小时,然后下一分钟,所以现在我们有了正确的顺序,您可以使用比较。我们尝试的是仅为时间数字创建可排序顺序。我正在寻找一个函数,该函数将取一个字符串参数,其值如“7:00 AM”或“7:00 PM”,并将其与当前时间进行比较。如果输入字符串时间晚于当天的当前时间,则返回true else false。你能用代码给我看一下吗?忘了提一下这是针对Swift 3.0和iOS的。是的,不太好,至少你告诉了你正在使用的lang平台。我能用不同的方法让它工作,包括将日期格式设为“无”选项。但你的方法从我看来也是有效的。我会附上我的解决方案,并给你信用。谢谢
//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