Convert String to Date in Swift

Convert string to date in Swift

  • Convert the ISO8601 string to date

      let isoDate = "2016-04-14T10:44:00+0000"

    let dateFormatter = DateFormatter()
    dateFormatter.locale = Locale(identifier: "en_US_POSIX") // set locale to reliable US_POSIX
    dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
    let date = dateFormatter.date(from:isoDate)!
  • Get the date components for year, month, day and hour from the date

      let calendar = Calendar.current
    let components = calendar.dateComponents([.year, .month, .day, .hour], from: date)
  • Finally create a new Date object and strip minutes and seconds

      let finalDate = calendar.date(from:components)

Consider also the convenience formatter ISO8601DateFormatter introduced in iOS 10 / macOS 10.12:

let isoDate = "2016-04-14T10:44:00+0000"

let dateFormatter = ISO8601DateFormatter()
let date = dateFormatter.date(from:isoDate)!

Converting a String to a Date in Swift

The date format needs to be for the string you are trying to convert, not the format you want back

so

formatter.dateFormat = "dd MM YYYY"

should be

formatter.dateFormat = "E, d MMM yyyy HH:mm:ss Z"

How to convert string to date without change time swift

You can add a timezone to dateFormatter

dateFromat.timeZone = TimeZone(secondsFromGMT: 0)

Updated code:

let scheduleDate : String = "2020-01-25 20:11:00"
let dateFromat : DateFormatter = DateFormatter()
dateFromat.dateFormat = "yyyy-MM-dd HH:mm:ss"
dateFromat.timeZone = TimeZone.init(secondsFromGMT: 0)
let dateFromString = dateFromat.date(from: scheduleDate)
print(dateFromString as Any)

Swift - iOS - Convert String to Date & issue

Your DateFormatter's dateFormat should be "MMM d, yyyy hh:mm:ss a"

("yyyy" instead of "YYYY" and "hh" instead of "HH")

That works.

Edit:

Or, as Leo pointed out in the comments, "MMM d, yyyy h:mm:ss a" (With only one h) would be better, and it's safer to use

dateFormatter.locale = Locale(identifier: "en_US_POSIX")

(That tells the date formatter not to try to adjust the date format for different local date formatting conventions.)

String Date to Date in Swift

You can use this Function

func dateFromString(_ dateString:String,  format: String = "yyyy-MM-dd HH:mm:ss.SSSSSSZ") -> String? {
let dateFormatter = DateFormatter()
let tempLocale = dateFormatter.locale // save locale temporarily
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.dateFormat = format//"yyyy-MM-dd HH:mm:ss.SSSSSS"
let date = dateFormatter.date(from: dateString)
dateFormatter.dateFormat = "dd-MM-yyyy HH:mm:ss"
dateFormatter.locale = tempLocale // reset the locale

guard let getdate = date else {
return nil
}

let dateString = dateFormatter.string(from: getdate)
return dateString
}

how to use

if let dateString = dateFromString("2020-05-05 10:19:25.357479+05:30") {

print (dateString)
}

Swift - converting string to date returning one day prior to correct date

Try This Out , it will work

func convertStringintoDate(dateStr:String, dateFormat format:String) -> Date{
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale.init(identifier:"en_US_POSIX")
dateFormatter.timeZone = TimeZone.init(abbreviation: "GMT")
dateFormatter.dateFormat = format
if let startDate = dateFormatter.date(from: dateStr){
return startDate as Date
}
return Date() as Date
}

Call this function like this :-

 print(convertStringintoDate(dateStr: "28/05/2020", dateFormat: "dd/MM/yyyy"))

Convert string containing date to Date

In addition to the Date being shown as an Optional, your format string appears to be wrong. "YYYY" should be "yyyy", so the whole line that assigns the formatter should be:

dateFormatter.dateFormat = "MMM d, yyyy hh:mma z"

That change yields the output

"Optional(2022-01-18 16:39:00 +0000)"

In addition, you should really force the calendar to Gregorian or iso8601, and set its locale to "en_US_POSIX:

An improved version of the date formatter could would look like this:

(from Leo's edit.)

let str = "Jan 18, 2022 04:39PM GMT"
let dateFormatter = DateFormatter()
dateFormatter.calendar = .init(identifier: .iso8601)
dateFormatter.locale = .init(identifier: "en_US_POSIX")
dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
dateFormatter.dateFormat = "MMM d, yyyy hh:mma z"

if let date = dateFormatter.date(from: str) {
let dateString = dateFormatter.string(from: date)
print(dateString == str) // true
}


Related Topics



Leave a reply



Submit