How to combine two strings (date & time) into a new date in Swift 3

前端 未结 1 1944
长情又很酷
长情又很酷 2021-01-06 05:15

I am trying to combine two date strings into a format that will allow for a new single date. The first string is a .long style format string. The second string is just a tim

1条回答
  •  有刺的猬
    2021-01-06 05:38

    let date = "March 24, 2017"
    let time = "7:00 AM"
    
    let dateFormatter = DateFormatter()
    dateFormatter.locale = Locale(identifier: "en_US_POSIX")
    dateFormatter.dateFormat = "MMMM dd, yyyy 'at' h:mm a"
    let string = date + " at " + time                       // "March 24, 2017 at 7:00 AM"
    let finalDate = dateFormatter.date(from: string)
    print(finalDate?.description(with: .current) ?? "")  // "Friday, March 24, 2017 at 7:00:00 AM Brasilia Standard Time"
    

    0 讨论(0)
提交回复
热议问题