How to Get Date from String in Swift 3

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)!

How to get Date from String in swift 3?

Military time (24-value hours) uses the capital 'H'. Try this for your formatting String:

dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss ZZZ"

How to convert date string into date in swift 3

Use this string class extension

   extension String {
func date(format: String) -> Date? {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = format
dateFormatter.timeZone =TimeZone.current
let date = dateFormatter.date(from: self)
return date
}
}

use above function like this

let date = "07/06/2017 11:23 AM".date(format:"MM/dd/yyyy HH:mm a")

Get Date from week number in Swift 3

You just have to convert the strings into Ints, then generate a DateComponents object from them, get a Date from the DateComponents through a Calendar and finally use a DateFormatter to get the expected String representation of the date.

Bear in mind that the Date object will represent the first second of the week of that year and hence the String representation will correspond to the first day of that week.

let yearString = "2017"
let weekOfYearString = "8"

guard let year = Int(yearString), let weekOfYear = Int(weekOfYearString) else {return}
let components = DateComponents(weekOfYear: weekOfYear, yearForWeekOfYear: year)
guard let date = Calendar.current.date(from: components) else {return}

let df = DateFormatter()
df.dateFormat = "yyyy-MM-dd"
let outputDate = df.string(from: date) //2017-02-19

how to convert array string to date swift 3

Because you're dateFormat string is wrong, you don't have a zone in your date strings (the Z stands for the time zone: http://www.unicode.org/reports/tr35/tr35-31/tr35-dates.html#Date_Format_Patterns)

This example here works perfectly fine:

let dates =  ["2017-10-30T07:41:00", "2017-10-30T11:23:00", "2017-10-30T11:48:00", "2017-11-10T00:00:00", "2017-11-13T19:43:00", "2017-12-01T00:00:00", "2017-12-31T00:00:00"]

let dateFormatter = DateFormatter()
dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
let dateObjects = dates.flatMap { dateFormatter.date(from: $0) }

print(dateObjects)

Output:

[2017-10-30 07:41:00 +0000, 2017-10-30 11:23:00 +0000, 2017-10-30 11:48:00 +0000, 2017-11-10 00:00:00 +0000, 2017-11-13 19:43:00 +0000, 2017-12-01 00:00:00 +0000, 2017-12-31 00:00:00 +0000]

How to convert string to date to string in Swift iOS?

First, you need to convert your string to NSDate with its format. Then, you change the dateFormatter to your simple format and convert it back to a String.

Swift 3

let dateString = "Thu, 22 Oct 2015 07:45:17 +0000"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "EEE, dd MMM yyyy hh:mm:ss +zzzz"
dateFormatter.locale = Locale.init(identifier: "en_GB")

let dateObj = dateFormatter.date(from: dateString)

dateFormatter.dateFormat = "MM-dd-yyyy"
print("Dateobj: \(dateFormatter.string(from: dateObj!))")

The printed result is: Dateobj: 10-22-2015

How can I convert string date to NSDate?

try this:

let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = /* find out and place date format from
* http://userguide.icu-project.org/formatparse/datetime
*/
let date = dateFormatter.dateFromString(/* your_date_string */)

For further query, check NSDateFormatter and DateFormatter classes of Foundation framework for Objective-C and Swift, respectively.

Swift 3 and later

let dateFormatter = DateFormatter()
dateFormatter.dateFormat = /* date_format_you_want_in_string from
* http://userguide.icu-project.org/formatparse/datetime
*/
guard let date = dateFormatter.date(from: /* your_date_string */) else {
fatalError("ERROR: Date conversion failed due to mismatched format.")
}

// use date constant here

Edit:

Alternative date time format reference
https://unicode-org.github.io/icu/userguide/format_parse/datetime/



Related Topics



Leave a reply



Submit