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

时间:2020-05-28 21:08:40

标签: swift string date character string-parsing

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

}

谢谢-欢迎所有反馈。

丹尼尔

2 个答案:

答案 0 :(得分: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 :(得分:1)

您可能应该使用现有的类型public partial class IndexArtworks { public IndexArtworks() { Artworks = new HashSet<Artworks>(); } public int Id { get; set; } public int IdArtwork { get; set; } public int IdArtist { get; set; } public bool AVisibleYn { get; set; } public string AVisibleIdAdmin { get; set; } public virtual Artists IdArtistNavigation { get; set; } public virtual Artworks IdArtworkNavigation { get; set; } public virtual ICollection<Artworks> Artworks { get; set; } } 来代替自己的类型。

要从格式化的字符串创建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