skip to Main Content

how i can implement this date format 1st July,2021 17:00.My current code is

func changeFormat(_ toFormat: String,_ dateStr: Date?) -> String{
        let date = dateStr ?? Date()
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = toFormat
        return dateFormatter.string(from: date)
    }
    
func covertStringToDate(_ date: String) -> Date {
        let isoDate = date

        let dateFormatter = DateFormatter()
        dateFormatter.locale = Locale(identifier: "en_US_POSIX") // set locale to reliable US_POSIX
        dateFormatter.dateFormat = "yyyy-MM-dd"
        return dateFormatter.date(from:isoDate)!
        
    }

2

Answers


  1. If u want to convert any Date object to this format do this.

    let date = Date()
    
    //create the date format without the day
    let dateFormatter = DateFormatter()
    dateFormatter.locale     = Locale(identifier: "en_US_POSIX")
    dateFormatter.dateFormat = "MMMM,yyyy HH:mm"
    let dateWithoutDay = dateFormatter.string(from: date)
    
    //get the day from the date
    let dayFormatter = DateFormatter()
    dayFormatter.locale     = Locale(identifier: "en_US_POSIX")
    dayFormatter.dateFormat = "d"
    let day = dayFormatter.string(from: date)
    
    //create a number formatter to get st/nd/rd/th suffix
    let numberFormatter = NumberFormatter()
    numberFormatter.numberStyle = .ordinal
    let dayWithSuffix = numberFormatter.string(from: NSNumber(value:Int(day)!))
    
    print("(dayWithSuffix!) (dateWithoutDay)") // --> 3rd September,2022 22:52
    

    If u want to convert this type of date (1st July,2021 17:00 ) to a Date object do this.

    let dateString = "1st July,2021 17:00"
    let dateSuffix = dateString.components(separatedBy: CharacterSet.decimalDigits).joined().prefix(2)
    let dateFormatter = DateFormatter()
    dateFormatter.locale     = Locale(identifier: "en_US_POSIX")
    dateFormatter.dateFormat = "dd'(dateSuffix)' MMMM,yyyy HH:mm"
    let date = dateFormatter.date(from: "1st July,2021 17:00")
    print(date!) // ->> 2021-07-01 11:30:00 +0000
    
    Login or Signup to reply.
  2. If you want to use localisation support, try this template(playground):

    import Foundation
    
    //let locale = Locale(identifier: "ar")
    //let locale = Locale(identifier: "en-us")
    let locale = Locale(identifier: "ja")
    
    let dayOfMonth: NSNumber = 11
    let dayPlaceholder = "dd"
    let dateFormat = DateFormatter.dateFormat(
        fromTemplate: "(dayPlaceholder)MMMM",
        options: 0,
        locale: locale
    )
    
    let numberFormatter = NumberFormatter()
    numberFormatter.locale = locale
    numberFormatter.numberStyle = .ordinal
    let dayOrdinalString = numberFormatter.string(from: dayOfMonth)
    
    let ordinalDateFormat = dateFormat?.replacingOccurrences(
        of: dayPlaceholder,
        with: "'(dayOrdinalString ?? "")'"
    )
    
    let formatter = DateFormatter()
    formatter.locale = locale
    formatter.dateFormat = ordinalDateFormat
    formatter.string(from: Date())
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search