0

I am using device time to get date components. this works fine for 12 hours formate but if user changes device time to 24 hours it shows wrong calculations. used the following method to convert but it always return nil.

            let dateAsString = "\(Date())"
            let df = DateFormatter()
            df.dateFormat = "yyyy-mm-dd HH:mm:ss zzz"

            let date = df.date(from: dateAsString)
            df.dateFormat = "d MMM yyyy h:mm a"

            let time12 = df.string(from: date!)
            print(time12)

5 Answers5

2

So, the core problem you're having is the fact that yyyy-mm-dd is using mm which is minutes and not using MM which is months.

So, if instead, you tried something like...

let dateAsString = "\(Date())"
let df = DateFormatter()
df.dateFormat = "yyyy-MM-dd HH:mm:ss zzz"

let date = df.date(from: dateAsString)
df.dateFormat = "d MMM yyyy h:mm a"

let time12 = df.string(from: date!)

it would result in

21 Feb 2020 6:00 PM

Now, the problem is, you could simply do

let df = DateFormatter()    
df.dateFormat = "d MMM yyyy h:mm a"
df.string(from: Date())

and get the same result.

Remember, Date does not, in of itself, have a concept of "format", beyond what its "debug" output provides, so you can't change a Date's format, instead, you use a formatter to "represent" the value of the Date in some human readable form.

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
1

I have just tried below it works fine on both cases. with AM or PM or 24Format and if you remove a in formatter.dateFormat = "dd MMM yyyy HH:mm a" gives you 24 Format. Even I am using this in my code.

let someDate = Date()
let formatter = DateFormatter()
formatter.dateFormat = "dd MMM yyyy HH:mm a"
let someDateTime = formatter.string(from: someDate)
print(someDateTime)

Print:

21 Feb 2020 09:35 AM

Newbie
  • 360
  • 3
  • 19
1

Try this:

let date : "2021-01-20T21:40:59.416Z"

func setDate(date: String) -> String {
   var interval: Double = 0
   let convertedDate = date.components(separatedBy: ":")

   for (index, part) in convertedDate.reversed().enumerated() {
       interval += (Double(part) ?? 0) * pow(Double(60), Double(index))
   }

   let date = Date(timeIntervalSinceNow: interval)

   let formatter = Foundation.DateFormatter()
   formatter.dateFormat = "LLLL dd, HH:mm at"
   return formatter.string(from: date)}

This will return a date in this format --> "January 21, 05:30 am"

Damien
  • 21
  • 2
0

Try,

let str = "2020-02-21 06:07:21 +0000"
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-mm-dd hh:mm:ss zzz"
if let date = formatter.date(from: str) {
    formatter.dateFormat = "dd MMM yyyy hh:mm a"
    let formattedDate = formatter.string(from: date)
    print(formattedDate)
}
PGDev
  • 23,751
  • 6
  • 34
  • 88
0

Swift-this answer helped me

swift - how to convert date from am/pm to 24 hour format

// posting my work here aswell.

  1. i had device formate in 24H.
  2. i needed in 12H.
  3. so i just converted date object to required formate with this code snippet. let df = DateFormatter() df.dateFormat = "d MMM yyyy h:mm a" let time12 = df.string(from: Date()) print(time12)