简体   繁体   中英

How to compare two time string check if string2 is grater then first string in swift

I am new in swift I search this questing and also got answer but not worked for me. Sorry for asking this again. My question is how to compare two time string. I am using this code.

let date = Date()
        let formatter = DateFormatter()
        formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
        let result = formatter.string(from: date)
        print(result)
        
        var totalTimeLoggedIn = TimeInterval() // 1 hour
        
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
        
        let loginTimeDate = dateFormatter.date(from: lblWorkStarted.text ?? "")!
        let logoutTimeDate = dateFormatter.date(from: result)!
        
        
        let timeLoggedIn = logoutTimeDate.timeIntervalSince(loginTimeDate)
        totalTimeLoggedIn += timeLoggedIn
        print("totalTimeLoggedIn: \(totalTimeLoggedIn.stringValue())")
        let TotalTimeLoginString = totalTimeLoggedIn.stringValue()
        let anotherdate = "02:00:00"
        
        if anotherdate < TotalTimeLoginString{
            print("True")
            
        }else{
            
        } 

lblWorkStarted.text is the current date and time. I am facing problem is that if TotalTimeLoginString is "10:15" some value like this then condition is not working properly and if TotalTimeLoginString is "03:12:11" some value like this then it is working properly. My problem is if I did not get hour then condition is not working. Thanks in Advance!

you have some mistakes here:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM