【问题标题】:Converting a string date to useful date将字符串日期转换为有用的日期
【发布时间】:2020-09-16 08:24:14
【问题描述】:

我对编程很陌生,我正在使用具有日期字段的数据库。字段中的所有数据都保存为 yyyymmdd 格式的字符串。我想将它们转换为更友好的东西以显示给用户。我创建了以下结构和函数来做到这一点。它有效,但我想知道是否有任何更简单的想法。

struct MyDate {
   var day: String
   var month: String
   var year: String
}

func ConvertToUsefullDate (_ date: String) -> MyDate {
    var myDate: MyDate = .init(day: "", month: "", year: "")

    myDate.year =
    String(date[date.index(date.startIndex, offsetBy: 0)]) +
    String(date[date.index(date.startIndex, offsetBy: 1)]) +
    String(date[date.index(date.startIndex, offsetBy: 2)]) +
    String(date[date.index(date.startIndex, offsetBy: 3)])

myDate.month =
    String(date[date.index(date.startIndex, offsetBy: 4)]) +
    String(date[date.index(date.startIndex, offsetBy: 5)])

myDate.day =
    String(date[date.index(date.startIndex, offsetBy: 6)]) +
    String(date[date.index(date.startIndex, offsetBy: 7)])

switch myDate.month {
    case "01": myDate.month = "Jan"
    case "02": myDate.month = "Feb"
    case "03": myDate.month = "Mar"
    case "04": myDate.month = "Apr"
    case "05": myDate.month = "May"
    case "06": myDate.month = "Jun"
    case "07": myDate.month = "Jul"
    case "08": myDate.month = "Aug"
    case "09": myDate.month = "Sep"
    case "10": myDate.month = "Oct"
    case "11": myDate.month = "Nov"
    case "12": myDate.month = "Dec"
    default: myDate.month = "Invalid"
}

return myDate

}

谢谢 - 欢迎所有反馈。

丹尼尔

【问题讨论】:

    标签: swift string date character string-parsing


    【解决方案1】:

    有一个现有的类型 Date 你应该使用它来代替你自己的类型。

    要从格式化字符串创建Date 对象,请使用DateFormatter

    let inputDateString = "20200428"
    
    let inputFormatter = DateFormatter()
    inputFormatter.dateFormat = "yyyyMMdd"
    inputFormatter.locale = Locale(identifier: "en_US_POSIX")
    
    // date is optional
    let date = inputFormatter.date(from: inputDateString) 
    

    一旦你有了一个Date 对象,你就可以用你喜欢的任何格式输出它:

    let outputFormatter = DateFormatter()
    
    // 28 April, 2020
    outputFormatter.dateFormat = "dd MMMM, yyyy" 
    

    或者,更好的是,使用区域感知方式:

    let outputFormatter = DateFormatter()
    outputFormatter.dateStyle = .long
    outputFormatter.timeStyle = .none
    

    然后就可以转成String了:

    let outputDateString = outputFormatter.string(from: date!) // ! for simplicity
    
    print(outputDateString)
    

    【讨论】:

    • 谢谢大家!那些很棒。我不知道我们可以反过来使用 DateFormatter()。从类型 Date 到 String 我知道,而不是相反。荣誉
    【解决方案2】:

    试试这个:

    let unformattedDate = your unformatted date
    
    let dateFormatter = DateFormatter()
    
    dateFormatter.dateFormat = "yyyy-MM-dd" (or whatever your current format is)
    let newDate : NSDate = dateFormatter.date(from: unformattedDate!)! as NSDate
    

    //新的日期格式,换成你喜欢的格式(我喜欢MM/dd/yyyy)

    dateFormatter.dateFormat = "MM/dd/yyyy"
    let formattedDate = dateFormatter.string(from: dateFromStringstartDate as Date)
    

    那么你可以随意使用formattedDate

    用 MMMM 表示月份

    使用 dddd 表示星期几

    用dd表示天

    用 yyyy 表示年份

    并使用 HH:mm:ss 来表示时间

    更多格式请查看下图! image of all formats

    【讨论】:

      猜你喜欢
      • 2011-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-27
      相关资源
      最近更新 更多