This question already has answers here:
NSDate is 5 hours off
(4 answers)
Closed 6 years ago.
I'm having trouble converting date String to NSDate
Here is my date string:
Fri, 08 Apr 2016 16:59:35 +0300
Trying to convert to NSDate:
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "EEE, dd MMM yyyy HH:mm:ss Z"
let dateFromString : NSDate = dateFormatter.dateFromString("Fri, 08 Apr 2016 16:59:35 +0300")
Return NSDate is:
2016-04-08 13:59:35 +0000
Timezone is wrong. Anyone can help with that?
Try this:
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "EEE, dd MMM yyyy HH:mm:ss Z"
NSTimeZone.setDefaultTimeZone(NSTimeZone(forSecondsFromGMT: 0))
let dateFromString : NSDate = dateFormatter.dateFromString("Fri, 08 Apr 2016 16:59:35 +0300")!
Note that it does not account for DST.
Related
Code for playground:
var dateStr = "Fri, 03 Aug 2018 08:55:22 GMT"
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.setLocalizedDateFormatFromTemplate("EEE, dd MMM yyyy HH:mm:ss z")
let date = dateFormatter.date(from: dateStr)
In the same time I use the online service - it shows that the format I chose is correct. Where is my mistake?
Small tip when you are working with DateFormatter, if it doesn't work one way, then do it the other way:
let string = dateFormatter.string(from: Date())
print("string: \(string)")
This way you'll see how your format is really interpreted.
If you do so, you'll get:
$> string: Fri, Aug 03, 2018, 12:05:09 GMT
See the extras , and the Aug 03 vs 03 Aug?
Now, your issue is that you misunderstood localizedDateFormatFromTemplate.
In fact you could remove spaces, punctuations and order.
For instance, dateFormatter.setLocalizedDateFormatFromTemplate("ddyyyyHHmmsszEMMM") gives the same output.
You just needs to give it what kind of infos you want (day, year, months, etc.), and it create the correct dateFormat corresponding in the localized version (adding then extra punctuations, etc.).
So use instead:
dateFormatter.dateFormat = "EEE, dd MMM yyyy HH:mm:ss z"
Side note:
I currently live in GMT+2, so I added dateFormatter.timeZone = TimeZone(secondsFromGMT: 0) to make it work.
You have to give dateFormatter.dateFormat to convert the string to date format. From the comment you get the answer but for others i am posting a correct answer here.
var dateStr = "Fri, 03 Aug 2018 08:55:22 GMT"
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.dateFormat = "EEE, dd MMM yyyy HH:mm:ss z" //Added this line.
let date = dateFormatter.date(from: dateStr)
I'm trying to convert string to NSDate. I suppose that my dateFormat is ok, but i still receive nil.
My code:
let dateStr = tweet.objectForKey("created_at") as! String
print(dateStr)
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "E MMM dd HH:mm:ss Z yyyy"
let date = dateFormatter.dateFromString(dateStr)
print(date)
Output:
Mon Apr 04 20:12:54 +0000 2016
nil
Mon Apr 04 14:17:09 +0000 2016
nil
Mon Apr 04 14:07:32 +0000 2016
nil
Sun Feb 21 12:37:23 +0000 2016
nil
I am using the same code and it's working for me. Assuming, you are getting correct value for dateStr as it is shown in Output.
let dateStr = "Mon Apr 04 20:12:54 +0000 201"
print(dateStr)
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "E MMM dd HH:mm:ss Z yyyy"
let date = dateFormatter.dateFromString(dateStr)
print(date!)
Output:
Mon Apr 04 20:12:54 +0000 2016
2016-04-04 20:12:54 +0000
See attached screenshot
I want to convert this string to NSDate,
Mon, 21 Mar 2016 10:26:45 GMT
so I set the date format to this:
dateFormatter.dateFormat = "EEE, dd MMM yyyy HH:mm:ss zzz"
but it returns nil.
I tried to change this format to:
E, dd MMM yyyy HH:mm:ss zzz
EEE, dd MMM yyyy hh:mm:ss zzz
EEE, d MMM yyyy HH:mm:ss zzz
but it also returns nil.
Which date format should I use?
Your format string is ok but you have to add a compatible locale to the formatter.
For example your string works well with the US locale:
let source = "Mon, 21 Mar 2016 10:26:45 GMT"
let dateFormatter = NSDateFormatter()
dateFormatter.locale = NSLocale(localeIdentifier: "en_US")
dateFormatter.dateFormat = "EEE, dd MMM yyyy HH:mm:ss zzz"
let result = dateFormatter.dateFromString(source)
Try this code block :
let dateFormatter = NSDateFormatter()
let dateString = "Mon, 21 Mar 2016 10:26:45 GMT"
dateFormatter.dateFormat = "EEE, dd MMM yyyy HH:mm:ss zzz"
let date = dateFormatter.dateFromString(dateString)
Try escaping the comma:
E',' dd MMM yyyy HH:mm:ss zzz
You can try the following code
let dateString:String = "Mon, 21 Mar 2016 10:26:45 GMT"
let dateFormat = NSDateFormatter.init()
dateFormat.dateStyle = .FullStyle
dateFormat.dateFormat = "ccc, dd MM yyyy HH:mm:ss zzz"
let date:NSDate? = dateFormat.dateFromString(dateString)
print(dateFormat.stringFromDate(date!))
For more info please visit NSDateFormatter formatting
I have an API sending date in string with universal time but when I try to convert to NSDate format it tends to return nil. I have looked in lot of places but couldn't find a way to resolve this. Below is my code that I have written. Please help me to find where am I going wrong. The string I'm trying to convert is "Sun Feb 14 23:35:40 UTC 2016". The problem causing it to return nil is with the time zone I believe.
let dateFormatter = NSDateFormatter()
dateFormatter.timeZone = NSTimeZone(abbreviation: "UTC")
dateFormatter.dateFormat = "EEE MMM dd hh:mm:ss yyyy"
dateFormatter.dateFromString("Sun Feb 14 23:35:40 UTC 2016")
Like this:
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "EEE MMM dd kk:mm:ss zzz yyyy"
let d = dateFormatter.dateFromString("Sun Feb 14 23:35:40 UTC 2016")
This question already has an answer here:
DateFormatter's returns nil for specific date strings without time in Swift
(1 answer)
Closed 7 years ago.
NSDateFormatter doesn't seem to like October Eighteenth. Tried every other date with success. (???)
It is almost certainly a bug in swift 2.0 (and if it is what should I do? I need to submit my app really soon). Am I missing something?
Checked it in playground on Xcode 7.0.1:
let str = "2015-10-18"
let str2 = "2015-10-14"
let formatter = NSDateFormatter()
formatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
formatter.dateFormat = "yyyy-MM-dd"
var date = formatter.dateFromString(str)
var date2 = formatter.dateFromString(str2)
Output:
"2015-10-18"
"2015-10-14"
<NSDateFormatter: 0x7f8aa050b760>
<NSDateFormatter: 0x7f8aa050b760>
<NSDateFormatter: 0x7f8aa050b760>
nil
"Oct 14, 2015, 12:00 AM"
The behavior depends on which timeZone the formatter using.
For example: America/Sao_Paulo
In Sao Paulo, http://www.timeanddate.com/time/change/brazil/sao-paulo?year=2015
Sunday, 18 October 2015, 00:00:00 clocks are turned forward 1 hour to
Sunday, 18 October 2015, 01:00:00 local daylight time instead
That means, there is no 2015-10-18 00:00:00 in Sao Paulo.
And as for NSDateFormatter, when it receives no time information from string, it assumes the time is 00:00:00 in its timezone. That's why it returns nil.
let formatter = NSDateFormatter()
formatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
formatter.timeZone = NSTimeZone(name: "America/Sao_Paulo")
formatter.dateFormat = "yyyy-MM-dd HH:mm"
formatter.dateFromString("2015-10-18 00:00") // -> nil
formatter.dateFromString("2015-10-18 00:30") // -> nil
formatter.dateFromString("2015-10-18 01:00") // -> non nil
As #LeoDabus stated, when you specify .calendar on the formatter:
it assumes the time is 00:00:00 in its timezone.
it will assume the start of the day for that date.
let formatter = NSDateFormatter()
formatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
formatter.timeZone = NSTimeZone(name: "America/Sao_Paulo")
formatter.dateFormat = "yyyy-MM-dd"
formatter.calendar = NSCalendar(identifier: NSCalendarIdentifierGregorian)
formatter.dateFromString("2015-10-17") // -> Oct 17, 2015, 12:00 AM
formatter.dateFromString("2015-10-18") // -> Oct 18, 2015, 1:00 AM
formatter.dateFromString("2015-10-19") // -> Oct 19, 2015, 12:00 AM