将字符串日期转换为有效日期

问题描述 投票:1回答:3

我对编程非常陌生,正在使用具有日期字段的数据库。字段中的所有数据均以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
3个回答
1
投票

尝试一下:

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


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

然后您可以将其转换为字符串:

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

print(outputDateString)

-1
投票

您需要使用dateFormatter代替此结构

let dateString = "20120621" // if its you date
let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "yyyyMMdd"
let date  = dateFormatter.date(from: dateString)

     dateFormatter.dateFormat = "dd MMM , yyyy"

  if let getDate = date {

        let formattedDate = dateFormatter.string(from: getDate )
          print(formattedDate) // prints 21 Jan , 2012

      }

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.