Swift中下一个可用日期的日期格式[重复]

Posted

技术标签:

【中文标题】Swift中下一个可用日期的日期格式[重复]【英文标题】:Date formatting for next available date in Swift [duplicate] 【发布时间】:2017-09-17 12:59:31 【问题描述】:

我目前正在从运行联盟的第三方网站下载体育应用程序的赛程列表,因此我必须使用的数据受到限制。

我正在尝试实现显示下一个即将到来的灯具的功能。 我的问题是检索的日期如下所示:

例如“2017 年 9 月 9 日星期六”和“18 年 3 月 24 日星期六”。

我在 DateFormatter 中尝试了许多我知道的日期格式,但找不到使用这种特定格式的任何内容。

如果我尝试在日期格式化程序中将 Date from string 方法与上述字符串之一一起使用,我会得到一个 nil 值。

我有一组固定装置,每个都有自己的日期。我需要将这些中的每一个与当前日期进行比较,以确定下一个日期。

我对此的临时解决方法是循环遍历灯具,一旦没有结果就将其显示为下一个灯具。显然,无论出于何种原因可能没有玩游戏时,这不起作用。

解决这个问题的最佳方法是什么?

【问题讨论】:

显示您尝试过的内容以及遇到的问题。 你可以使用 NSDataDetector。 ***.com/questions/32595651/… 如果您不想使用 NSDataDetector,则在尝试解析日期字符串“EEE d'st' MMM yy”时需要使用 3 种不同的 dateFormat(st、nd 和 th),@ 987654322@ 或“EEE d'th' MMM yy” 我使用了 NSDateDetector,效果很好,谢谢! 不客气 【参考方案1】:

基本上,您只需将当前日期转换为与您从第三方网站获得的日期相同的格式(或相反),以便您轻松比较它们:

    let currentDate = Date()

    let dateFormatter = DateFormatter()

    dateFormatter.dateFormat = "EE MMM"
    let firstPartStringDate = dateFormatter.string(from: currentDate)

    dateFormatter.dateFormat = "yy"
    let lastPartStringDate = dateFormatter.string(from: currentDate)

    let day = Calendar.current.component(.day, from: currentDate)

    let formatter = NumberFormatter()
    formatter.numberStyle = .ordinal

    guard let ordinalDay = formatter.string(for: day) else 
        return
    

    let finalDateString = firstPartStringDate + " \(ordinalDay) " + lastPartStringDate

    print(finalDateString)

对于今天的当前日期,您将获得与从第三方网站获得的完全相同的格式:

Sun Sep 17th 17

更新: 以下是如何将您从第三方网站获得的String 转换为Date,然后将其与当前日期进行比较。这样首先解决了String里面st、nd、rd、th的问题。

// This is the string you get from the website    

    var webDateString = "Sat 9th Sep 17"

// First, remove the st, nd, rd or th if it exists :

    if let stSubrange = webDateString.range(of:"st") 
        webDateString.removeSubrange(stSubrange)
     else if let ndSubrange = webDateString.range(of:"nd") 
        webDateString.removeSubrange(ndSubrange)
     else if let rdSubrange = webDateString.range(of:"rd") 
        webDateString.removeSubrange(rdSubrange)
      else if let thSubrange = webDateString.range(of:"th") 
        webDateString.removeSubrange(thSubrange)
    

// Now convert the string to a date :   

    dateFormatter.dateFormat = "EE MMM dd yy"
    guard let formattedDate = dateFormatter.date(from: finalDateString) else 
        return
     

// You can now compare formattedDate and the current date easily like so :

    let currentDate = Date()

    if formattedDate < currentDate 

        // Do something interesting here :)

     else 

        // Do something else!
    

【讨论】:

无需创建新的 NSNumber 对象,可以使用 string(for:) 代替 string(from:) formatter.string(for: day)。顺便说一句,OP 想要获取日期而不是字符串 这只是一个额外的步骤,因为我们需要在这里将日期作为一个值而不是字符串,在通过 NumberFormatter 添加后缀之后立即完成到字符串的转换。 哦,确实很抱歉,感谢您指出这一点。我会更新答案,

以上是关于Swift中下一个可用日期的日期格式[重复]的主要内容,如果未能解决你的问题,请参考以下文章

Swift 日期格式化程序错误地转换/创建日期/字符串 [重复]

如何在 Swift 中将此日期字符串格式“2018-03-30T14:36:10.093”格式化为日期 [重复]

如何在swift(ios)中将字符串格式的日期转换为另一种字符串格式[重复]

这是啥日期时间格式?即:“1551927028”[重复]

常用方法 读取 Excel的单位格 为 日期格式 的数据

在swift 3中将字符串转换为日期类型[重复]