首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >只需要简单地将时间字符串与当前日期时间值进行比较。

问只需要简单地将时间字符串与当前日期时间值进行比较。
EN

Stack Overflow用户
提问于 2017-04-03 17:37:05
回答 2查看 204关注 0票数 0

说时间字符串值是"7:00 AM“,称之为提醒时间。

现在,我需要做的就是比较这个时间和当前的日期时间,如果提醒时间比当前时间晚的话,就说它的"9:00 AM“--返回true AM false。这是日期格式化器的格式"h:mm a“。

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

我只想比较两个时间值,并确定第一个是晚一点还是在第二个之后。日期总是今天或当前日期,所以我只关心日期的时间部分。当然,您必须转换为日期来进行比较,但是当前日期很容易得到,但是从"7:00 AM“字符串中得到的日期在比较中似乎不正确。

有人有这个功能吗?

谢谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 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)和相应的时间。

  • 2000-01-01 7:00:00 =>这将是您的上午7:00与默认日期
  • 2000-01-01 15:56:00 =>这将是默认日期的当前时间。

现在我们将比较两个日期对象。

检查小提琴 小提琴

代码语言:javascript
复制
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);
票数 1
EN

Stack Overflow用户

发布于 2017-04-04 14:18:50

我的解决办法如下。

私有函数ReminderAfterCurrentTime( reminderTimeString : String) -> Bool {//比较两个时间字符串,如果reminderTimeString比当前时间字符串//返回真,因为提醒是在当前时间之后。

代码语言:javascript
复制
//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

}

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43190946

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档