0

Here is the thing. I have my iPhone in am/pm format and iPhone Language in Spanish(MX). when I ask for the date, I get something like 2018-03-21 5:14:59 a. m. +0000 and it's a date type, then I try to convert it to string in 24hours format, and I get something like 2018-03-20 23:14:59 and it's a String type, BUT when I try to convert that string into date, I get the same date in am/pm format again 2018-03-21 5:14:59 a. m. +0000 I don't know what else to do, all I want is convert my am/pm date to 24hours date NOT STRING. Help me please. Here is my code in Swift 4

    let todayDate = Date()
    print("todayDate: \(todayDate)")
    let dateFormatter = DateFormatter()
    dateFormatter.locale = Locale(identifier: "en_US_POSIX")
    dateFormatter.dateFormat = "yyyy-MM-dd' 'HH:mm:ss"
    let stringDate = dateFormatter.string(from: todayDate)
    print("stringDate: \(stringDate)")
    let dateFromString = dateFormatter.date(from: stringDate)
    print("dateFromString: \(dateFromString!)\n\n\n")

this is my console results

    todayDate: 2018-03-21 5:14:59 a. m. +0000
    stringDate: 2018-03-20 23:14:59
    dateFromString: 2018-03-21 5:14:59 a. m. +0000
rmaddy
  • 314,917
  • 42
  • 532
  • 579
jorge_mr
  • 11
  • 2
  • https://github.com/melvitax/DateHelper – PPL Mar 21 '18 at 05:34
  • A `Date` object doesn't have a internal concept of a format, it's just a container for the period of time from a anchor point. `dateFromString` is just printing the `Date`s implementation of `description`, you need to format the `Date` value again back to `String` – MadProgrammer Mar 21 '18 at 05:40
  • for which country you are trying to do ? and what is your country both are same ? – Ravi Panchal Mar 21 '18 at 05:41
  • 1
    There is no reason to try to do what you are trying to do. Your `Date` object is fine as it is. Don't worry about how it looks when you use `print` on the `Date` object. Only worry about the output when you actually wish to show the date as a `String` using a `DateFormatter`. – rmaddy Mar 21 '18 at 05:42
  • I have a initDate and an finishDate in the database (Realm) and Im trying to validate that todayDate is within that range. But the dates in realm are in 24hours format and when todayDate is in AM/PM format I can't compare these dates. when todatDate is in 24 hours format everything is fine. – jorge_mr Mar 21 '18 at 05:51
  • `todayDate` is not in am/pm format. It's a `Date`. It has no format. If you are having issues with comparing dates, post a question specific to that issue. Show how you get your dates from Realm. Show your attempt to compare the dates. – rmaddy Mar 21 '18 at 05:52

1 Answers1

4

all I want is convert my am/pm date to 24hours date NOT STRING

That's not how Date works.

From the Documentation

Date values represent a time interval relative to an absolute reference date.

Date is simply a container for the period of time since the reference date, it does not carry any kind of formatting information itself.

let dateFromString = dateFormatter.date(from: stringDate)
print("dateFromString: \(dateFromString!)\n\n\n")

is simply converting the stringDate (2018-03-20 23:14:59 in your example) back to Date object and print is using the Dates description implementation to provide you with information about it's current value

So, if we instead added...

print("dateFromString: \(dateFormatter.string(from: dateFromString!))\n\n\n")

It would print 2018-03-21 16:44:17

Your best bet is not to care. Simply carry the value around in a Date object and format it to String when you need to display it to the user or otherwise need it formatted - that's the point of having formatters

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366