Convert string to date in Swift/SwiftUI




How can I convert this string "2021-06-14T19:07:41.183000" into an NSDate and keep only the year, month, day, hour? The T in the middle of it.

"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"


Format:

  • year, month, day, as "XXXX-XX-XX"
  • the letter "T" as a separator
  • hour, minute, seconds, milliseconds, as "XX:XX:XX.XXX".
  • the letter "Z" as a zone designator for zero offset, a.k.a. UTC, GMT, Zulu time.
The goal is a string that looks like this:

"2021-06-14T19:07:41.183000" 


Create a var  order_date return type as String? --

var order_date : String? {
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
        let date = dateFormatter.date(from: placedDate)
        dateFormatter.dateFormat = "dd MMM, yyyy"
        let dateString = dateFormatter.string(from: date!)
        return dateString
    }


And here is output--

14 Jun, 2021



Thanks for reading!!

Similar solutions you may also like...

Post a Comment

1 Comments

  1. PokerStars | Bet with the best online casino players
    Bet with the best online casino players. Our world class PokerStars sportsbook 메리트 카지노 쿠폰 is your ticket to a world of Poker. 카지노사이트 Place 온카지노 bets on a huge range of

    ReplyDelete