How to convert a string to Date in Swift2 2.0 - ios

I am working on convert a date string to NSDate, but I keep getting nil:
let strDate = self.DateList[0] // "2015-10-06T15:42:34Z"
print(strDate)
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
print (dateFormatter.dateFromString(strDate))
And the running result is:
2016-05-18T14:00:54.466Z
nil
I dont know why, any suggestions?

You got 2016-05-18T14:00:54.466Z in a print(strDate)
so, Your format should be yyyy-MM-dd'T'HH:mm:ss.SSS'Z'
//---------------------------------2016-05-18 T 14:00:54.466 Z ----
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
print (dateFormatter.dateFromString(strDate))

You almost had it - just needed to escape out the 'Z':
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss'Z'"

A safer solution would be to use ZZZZZ as the time zone specifier or you could use of of the X specifiers.
The difference between Z and X is basically that X allows for literal Z to be used when the time offset is zero. See Date Field Symbol Table
Of course, you are also missing the millisecond part (.SSS). If you have two formats, one with milliseconds and one without milliseconds, you will have to use two different formatters.

Related

Formatting JSON date to Swift date doesn't work

I'm trying to format this date: 2018-01-10T11:57:21.153 to Swift Date object like this:
let dateSentString = jsonDict["date"] as! String
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
let date = dateFormatter.date(from: dateSentString)!
For some reason, the app crashes on the last line.
What am I doing wrong? Thanks!
change the milli seconds format use 'SSS' specifier (with number of S's equal to number of digits of milliseconds ). for more information you get here
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS"
from
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
Full code
let dateSentString = "2018-01-10T11:57:21.153"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS"
let date = dateFormatter.date(from: dateSentString)!
print(date)
You have to first set formatter for date you are getting from JSON and then another formatter for the date you want.
First convert string fro JSON to a date variable by setting same format coming in JSON object .
Then you have to re-format that date variable into format you want.
I can write code if you want but it is better to try yourself.
Happy Coding

Dateformat of 2017-12-16T07:28:59.629Z

What date format I need to process this string? 2017-12-16T07:28:59.629Z
Tried yyyy-MM-dd'T'HH:mm:ssZZZZZ and Tried yyyy-MM-dd'T'HH:mm:sssZZZZZ
Anyway how many s and Z needs end of the format?
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:sssZZZZZ"
date = dateFormatter.date(from: value as! String)
use the dateformat as
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
as the Date Format Patterns suggests that "S" is the format specifier for fractions of seconds.

What should be the format of a string to convert into NSDate?

I'm getting following two types of strings from server:
2016-07-28T12:25:31.922247
2016-07-28T13:39:13
I want to convert them into NSDate. I'm using following snippet to convert but it's failing:
let dateFormatter = NSDateFormatter()
dateFormatter.timeZone = NSTimeZone(name: "UTC")
dateFormatter.dateFormat = "yyyy-MM-ddTHH:mm:ss"
I'm not getting the desired output.
If you doesn't care the fraction of second then you can remove it like this.
var strDate = "2016-07-28T12:25:31.922247"
if strDate.rangeOfString(".") != nil{
let arr = strDate.characters.split{$0 == " "}.map(String.init)
strDate = arr[0]
}
//Now you can convert this string to date using same date format
let formatter= NSDateFormatter()
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
let date = formatter.dateFromString(strDate)
You can get the exact format from this Link
For the second string use this
yyyy-MM-dd'T'HH:mm:ss
You need to quote the "T" (or any alpha characters that should be present in literal form), try:
let dateFormatter = NSDateFormatter()
dateFormatter.timeZone = NSTimeZone(name: "UTC")
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
Note that this will only parse your second string. To parse your first string you'll need to use:
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.S"
if you care about the fractional seconds. Since NSDateFormatter is a literal parser it doesn't allow you to easily parse either format, if you have to parse both you'll just need to pass it to one, if that fails pass to the other.
Your date format works in 2016-07-28T13:39:13 but add 'T'
Example: yyyy-MM-dd'T'HH:mm:ss.
But for the 2016-07-28T12:25:31.922247 you need clarifies that this means 922247
the truth had never seen anything like it, but I would try with yyyy-MM-ddTHH:mm:ssZ

Why my date parser doesn't work in Swift?

I have a json that contains date field:
"created_at":"2016-03-06T16:39:29.786Z"
in my app I'm doing like this:
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-ddThh:mm:ss.SSSZ"
let created_at = json["created_at"].string
let crd = dateFormatter.dateFromString(created_at!)
print(crd) //prints nil
Why am I getting nil there?
You need to quote literal text and you are using the wrong format specifier for the hour.
The T must be quoted.
hh is for 12-hour format but you have 24-hour format which is HH.
So you want:
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"

How to convert String to NSDate?

I have string that I received whenever there is new remote notifications.I'm using parse for my Backend. And String that I retrieved come from "createdAt" column.
I've tried below code:
var ca = "2015-07-03T03:16:17.220Z"
var dateFormater : NSDateFormatter = NSDateFormatter()
dateFormater.dateFormat = "yyyy-MM-dd'T'HH:mm:ss'Z'"
let date = dateFormater.dateFromString(ca)
println(date)
But the println is giving me nil, I think there is something wrong with my date format. How can I fix this?
You are missing the milliseconds. Thus:
dateFormater.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
Note, when converting from the date string to a NSDate, you shouldn't quote the Z. If you quote the Z, it will match the literal Z character, but won't correctly reflect that this date string is actually Zulu/GMT/UTC.
If you want to create formatter that also goes the other way, converting NSDate objects to strings, in that case you should quote the Z, but in that case you must remember to explicitly set the timezone:
dateFormater.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
dateFormater.timeZone = NSTimeZone(forSecondsFromGMT: 0)
By the way, don't forget to set the locale as per Apple Technical Q&A 1480.
dateFormater.locale = NSLocale(localeIdentifier: "en_US_POSIX")

Resources