Convert string to date in Swift

convert string to date in swiftui
convert string to date in swift - stack overflow
convert string to time swift
swift date
convert int to date swift
swift check if string is date
convert utc string to date swift
cannot assign value of type 'date?' to type 'string?'

How can I convert this string "2016-04-14T10:44:00+0000" into an NSDate and keep only the year, month, day, hour?

The T in the middle of it really throws off what I am used to when working with dates.

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  Converting a Date instance to a string using one of the built-in date formats. Converting a Date instance to a string using one of the built-in time formats. Converting a Date instance to a string using a completely custom format. Converting a string instance to a Date. Below are examples of each to get you started.

Try the following date Format.

let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ssZZZ"
let date = dateFormatter. dateFromString (strDate)

Hope it helps..

Swift 4.1 :

let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ssZZZ"
let date = strDate)

Converting Date String to Date Type with Swift - Luis Arias, This is my first post, my aim with this series of very small “tutorials” is to share with everyone out there my knowledge about Swift/iOS/watchOS  Convert string to DATE type in swift 3 [duplicate] Ask Question Asked 3 years, 3 months ago. You can convert String Date into Date/NSDate like below code: -

In Swift 4.1 you can do:

func getDate() -> Date? {
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
    dateFormatter.timeZone = TimeZone.current
    dateFormatter.locale = Locale.current
    return "2015-04-01T11:42:00") // replace Date String

How to convert dates and times to a string using , If you want to get a string from a Date , Apple's DateFormatter class has everything you need: you can get short dates, long dates, dates with times  How to convert dates and times to a string using DateFormatter; About the Swift Knowledge Base. This is part of the Swift Knowledge Base, a free, searchable collection of solutions for common iOS questions.

Swift 3.0 - 4.2

import Foundation

extension String {

    func toDate(withFormat format: String = "yyyy-MM-dd HH:mm:ss")-> Date?{

        let dateFormatter = DateFormatter()
        dateFormatter.timeZone = TimeZone(identifier: "Asia/Tehran")
        dateFormatter.locale = Locale(identifier: "fa-IR")
        dateFormatter.calendar = Calendar(identifier: .gregorian)
        dateFormatter.dateFormat = format
        let date = self)

        return date


extension Date {

    func toString(withFormat format: String = "EEEE ، d MMMM yyyy") -> String {

        let dateFormatter = DateFormatter()
        dateFormatter.locale = Locale(identifier: "fa-IR")
        dateFormatter.timeZone = TimeZone(identifier: "Asia/Tehran")
        dateFormatter.calendar = Calendar(identifier: .persian)
        dateFormatter.dateFormat = format
        let str = dateFormatter.string(from: self)

        return str

How can i convert string back to Date format? : swift, dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ" let date = dateFormatter.​dateFromString(date)!. I used something like this but in swift 3, i can't get in  Swift Tip: String to Data and Back. Posted on February 13th 2018. When converting a string into data, the return type is optional. Strings in Swift use Unicode

Hi You have separate T Format and then convert as you like

// create dateFormatter with UTC time format
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
dateFormatter.timeZone = NSTimeZone(name: "UTC")
let date = dateFormatter.dateFromString("2015-04-01T11:42:00")

// change to a readable time format and change to local time zone
dateFormatter.dateFormat = "EEE, MMM d, yyyy - h:mm a"
dateFormatter.timeZone = NSTimeZone.localTimeZone() 
let timeStamp = dateFormatter.stringFromDate(date!)

Parsing and Formatting Dates in Swift, Parsing dates is what is referred to when we take a raw value (say a String or Int ) and convert it into a Date instance we can work with in our  You can convert unixTimestamp to date using Date(timeIntervalSince1970:).. let unixTimestamp = 1480134638.0 let date = Date(timeIntervalSince1970: unixTimestamp) If you want to display date in string with specific formate than you can use DateFormatter like this way.

How to Get NSDate from string using format in Swift, I have a Date string and want to convert it to an NSDate object, using a specific date format. Recipe. // Initialize Date string var dateStr = "2015-08-25"  let str = String(decoding: data, as: UTF8.self) If the Data instance can’t be converted to a UTF-8 string, you’ll get sent back an empty string. This means you do need to know which format is used to store the string, but UTF-8 is usually the best to go with.

Convert Date to String and String to Date using Swift3 – How to , import Foundation extension NSObject { /// Convert String to Date func convertToDate(dateString: String) -> Date { let dateFormatter  Swift: iOS if we have string, convert it to NSDate, var dataString = profileValue["dob"] as String var dateFormatter = NSDateFormatter() dateFormatter.dateFormat = "MM-dd-yyyy" // convert string into date let dateValue:NSDate? = dateFormatter.dateFromString(dataString) if you have and date picker parse date like this // to avoid any nil value

Convert Date To String & Vice-Versa Swift 4 - iOS, For converting string to date, we have to give the date format as the string. Add the following extension to String: extension String { func toDate  The string date I need to convert is in this format: March 30, 2019, so date formatter has been set to MMMM dd, yyyy. You can change it to whatever format you need to match your string input. Date formatter syntax is a bit weird the first time you see it, but you can learn more about different date formats from the Apple Developer Documentation

  • You want just Year , month , day , hour right ?
  • Possible duplicate of From String to NSDate in Swift
  • the Z in "yyyy-MM-dd'T'HH:mm:ssZ" solved what was not working for me
  • I just want year month and day, but this is not working for me
  • @Sneha If you don't want the hour just omit .hour
  • @vadian I did but I'm not getting
  • All in one: ISO8601DateFormatter().date(from:"your string")!
  • Tried it, and date return nil after this.
  • Cannot convert value of type 'String' to type 'Date' in coercion