比较没有日期的时间swift3 [英] comparing time without date swift3

查看:55
本文介绍了比较没有日期的时间swift3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用API​​来获取时间列表,该时间每天是(AM,PM)格式的字符串的五次,没有日期,

I am using an API to get a list of times which is five times a day as string in (AM,PM) format without date,

( 4 :30 AM, 1:00 PM, 3:20:PM, 6:40 PM, 9:10 PM)

("4:30 AM","1:00 PM","3:20: PM","6:40 PM","9:10 PM")

什么我想做的是显示屏幕上的下一次时间取决于iPhone上的当前时间

what I am trying to do is to show the next time in the screen depends on the current time on the iPhone

因此,如果现在的时间是下午5:00,则该时间应为屏幕上显示的时间是下午6:40

so if the time now is 5:00 PM the time should display on the screen is 6:40 PM

我如何快速比较时间而不包括日期,因为我试图将必须具有时间类型的时间列表转换为日期类型,但是不幸的是,日期和日期显示在2001年,所以即使我尝试比较日期也会给我错误的结果

how can I compare times in swift without including the dates because I tried to convert the list of times I have to Date type but unfortunately am getting dates and the dates shows in 2001 so even if I try to compare the date would give me wrong result

 print(self._praytime.arraytime)




            for Dates in self._praytime.arraytime {
                let dateformatters = DateFormatter()
                dateformatters.dateStyle = .none
                dateformatters.timeStyle = .short
                let xxbbs = dateformatters.date(from: Dates)
                print(xxbbs!)
            }

在获得时间后将其放入Array中,并尝试执行上面的代码,这样我可以得到没有日期的时间,但是输出屏幕上显示的是这个

after I got the times I put it in Array and I tried to execute the code upper so I can get the times without the date but what shows in the output screen was this

Optional(2000-01-01 11:14:00 +0000)
Optional(2000-01-01 20:06:00 +0000)
Optional(1999-12-31 23:57:00 +0000)
Optional(2000-01-01 03:11:00 +0000)
Optional(2000-01-01 04:41:00 +0000)

任何想法如何解决此问题以及是否有其他简便方法做这个任务,我很高兴听到它

any idea how to solve this problem and if is there any other easy way to do this task I'm exciting to hear it

谢谢大家

推荐答案

使用 DateComponents


  • p>

  • Assuming this given array

let times = ["4:30 AM","1:00 PM","3:20 PM","6:40 PM","9:10 PM"]


  • 创建日期格式r匹配格式

  • Create a date formatter matching the format

    let formatter = DateFormatter()
    formatter.dateFormat = "h:mm a"
    


  • 将时间字符串映射到 Date 并立即考虑小时分钟 DateComponents >

  • Map the time strings to Date and immediately to DateComponents considering only hour and minute

    let dateArray = times.map { Calendar.current.dateComponents([.hour, .minute], from:formatter.date(from:$0)!) }
    


  • 将此数组映射到下一个日期,从现在开始分别匹配组件

  • Map this array to the next date from now matching the components respectively

    let upcomingDates = dateArray.map { Calendar.current.nextDate(after: Date(), matching: $0, matchingPolicy: .nextTime)!  }
    


  • 将数组升序排列,第一项是您要查找的日期

  • Sort the array ascending, the first item is the date you are looking for

    let nextDate = upcomingDates.sorted().first!
    


  • 使用格式化程序将日期转换回时间字符串

  • Use the formatter to convert the date back to a time string

    print(formatter.string(from:nextDate))
    


  • 这篇关于比较没有日期的时间swift3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆