DateFormatter return nil in some case [duplicate] - ios

I have a problem with converting the string to date in swift 3. Here is my code, it returns me a nil value while converting.
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "EEE, dd MMM yyyy hh:mm:ss +zzzz"
dateFormatter.locale = Locale.init(identifier: "bg_BG")
let recdate = dateFormatter.date(from:"Fri, 10 Mar 2017 15:03:03 +0530")!;`

You set the wrong format specifier for hour and timezone. Use this:
dateFormatter.dateFormat = "EEE, dd MMM yyyy HH:mm:ss Z"
dateFormatter.locale = Locale(identifier: "en_US")
hh means 12-hour format so there's no hour 15. Use HH instead
+zzzz is invalid timezone specifier. Use Z instead
Unless Friday is shortened to Fri in Bulgarian, use an English locale

You have a couple of problems, first, as pointed out by Code Different, you need to be using HH to read 24-hour times. But, you're also specifying a locale, which means that the "word" portions must be in bulgarian, not english. Leaving the language the default seems to work fine:
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "EEE, dd MMM yyyy HH:mm:ss +zzzz"
//dateFormatter.locale = Locale.init(identifier: "bg_BG")
let recdate = dateFormatter.date(from:"Fri, 10 Mar 2017 15:03:03 +0530")!
If you were to use Bulgarian day and month names, your format should work.
dateFormatter.date(from:"нд, 10 март 2017 15:03:03 +0530")

Related

Date Formatter in swift

i want to get this type of formate -> April 20, 2018 at 9:02:00 PM UTC+5:30
for firestore timestamp. i already try this code but do not get proper formate as above.
let date = Date()
let formatter = DateFormatter()
formatter.dateFormat = "MMMM d yyyy hh:mm:ss a Z"
formatter.timeZone = NSTimeZone(name: "IST") as TimeZone?
let result = formatter.string(from: date)
print(result)
already use this formatter formatter.dateFormat = "MMMM d yyyy'T'hh:mm:ss a Z"
Thanks
You can use like this:
let date = Date()
let formatter = DateFormatter()
formatter.dateFormat = "MMMM dd, yyyy 'at' hh:mm:ss a 'UTC'Z" //If you dont want static "UTC" you can go for ZZZZ instead of 'UTC'Z.
formatter.timeZone = TimeZone(abbreviation: "IST")
let result1 = formatter.string(from: date)
print(result1)
Note: If you do not want the UTC to be static in the string, you can also use the format "MMMM dd, yyyy 'at' hh:mm:ss a ZZZZ"
Thank you Larme For the format improvement
Output:
do like
let formatter = DateFormatter()
//If you want it in your specific format you can simply add 'UTC'
formatter.dateFormat = "MMMM dd, yyyy 'at' hh:mm:ss a 'UTC'Z"
formatter.timeZone = NSTimeZone(name: "IST") as TimeZone?
let result = formatter.string(from: Date())
print(result)
Note: if you want to set the currentTimezone for your string then use the format "MMMM dd, yyyy 'at' hh:mm:ss a ZZZZ"
you get the output as
Try this if you don't want leading zero in hour, when hour has single digit. Also you don't need to typecast NSTimeZone into TimeZone. You can directly use TimeZone.
let date = Date()
let formatter = DateFormatter()
formatter.dateFormat = "MMMM dd, yyyy 'at' h:mm:ss a 'UTC'Z"
formatter.timeZone = TimeZone(abbreviation: "IST")
let result = formatter.string(from: date)
print(result)
April 23, 2018 at 3:09:01 PM UTC+0530

Swift: DateFormatter not able to parse date

I've a specific time format which I want to use to generate a Date object.
This is the format:
Fri, 17 Mar 2017 08:42:00 +0100
Here is my Swift code that should create the data object:
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "EEE, dd MMM yyyy HH:mm:ss xx"
let d = dateFormatter.date(from: value)
The generation of the Date object with dateFormatter.date does always return nil. But I cant see what's wrong in my formatting string.
Can anyone see my error?
I have test it and works perfectly.
Could you test this code line to identify you Locale configuration?
print(Locale.current.identifier)
I only made a little change related to the Optional result that DateFormatter method returns
import Foundation
let the_date: String = "Fri, 17 Mar 2017 08:42:00 +0100"
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale.current
dateFormatter.dateFormat = "EEE, dd MMM yyyy HH:mm:ss xx"
if let d = dateFormatter.date(from: the_date)
{
print(d)
}

Conversion from string to date in swift returns nil

I have a problem with converting the string to date in swift 3. Here is my code, it returns me a nil value while converting.
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "EEE, dd MMM yyyy hh:mm:ss +zzzz"
dateFormatter.locale = Locale.init(identifier: "bg_BG")
let recdate = dateFormatter.date(from:"Fri, 10 Mar 2017 15:03:03 +0530")!;`
You set the wrong format specifier for hour and timezone. Use this:
dateFormatter.dateFormat = "EEE, dd MMM yyyy HH:mm:ss Z"
dateFormatter.locale = Locale(identifier: "en_US")
hh means 12-hour format so there's no hour 15. Use HH instead
+zzzz is invalid timezone specifier. Use Z instead
Unless Friday is shortened to Fri in Bulgarian, use an English locale
You have a couple of problems, first, as pointed out by Code Different, you need to be using HH to read 24-hour times. But, you're also specifying a locale, which means that the "word" portions must be in bulgarian, not english. Leaving the language the default seems to work fine:
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "EEE, dd MMM yyyy HH:mm:ss +zzzz"
//dateFormatter.locale = Locale.init(identifier: "bg_BG")
let recdate = dateFormatter.date(from:"Fri, 10 Mar 2017 15:03:03 +0530")!
If you were to use Bulgarian day and month names, your format should work.
dateFormatter.date(from:"нд, 10 март 2017 15:03:03 +0530")

Date formatter returning nil value

I have this code working but now all of a sudden, it started crashing. I could not figure it out, any help would be appreciated.
let dateFormatter = NSDateFormatter()
dateFormat = "EEE, MMM d, yyyy hh:mm a"
dateFormatter.dateFormat = dateFormat
let date = dateFormatter.dateFromString("Thu, Jan 5, 2017 12:28 PM")
It is returning date as nil. Why? I want it to return proper date.
This looks like a possible duplicate question, but it is with specific details.
let dateformattor = NSDateFormatter()
dateformattor.dateFormat = "EEE, MMM d, yyyy hh:mm a"
dateformattor.timeZone = NSTimeZone.localTimeZone()
let dt = "Thu, Jan 5, 2017 12:28 PM"
let dt1 = dateformattor.dateFromString(dt as String)
print(dt1) //print date and time UTC format.
Output :
2017-01-05 06:58:00 +0000
var dateFormatter = DateFormatter()
let dateFormat = "EEE, MMM d, yyyy hh:mm a"
dateFormatter.dateFormat = dateFormat
let date = dateFormatter.date(from: "Thu, Jan 5, 2017 12:28 PM")
OUTPUT :
Every time you work with time and date formatters remember to set locale and timezone!
Device with non-english locale will fail to parse "Thu, Jan 5, 2017 12:28 PM" as "Thu" and "Jan" are obviously english specific.
So:
dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
or whatever time zone and locale you want to use

Converting String to NSDate Giving Wrong Date

I have a String that I converted using stringFromDate and now I'm trying to convert it back, however when the UIPicker starts, it's giving me the wrong day
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "dd MMM YYYY"
print(birthday) // logs 15 Jan 1992
let date = dateFormatter.dateFromString(birthday)
self.datePicker.setDate(date!, animated: true)
I tried hardcoding "15 Feb 1992" but still the same result. The date on UIDatePicker shows 22 Dec 1991 on Start.
If I use hardcore 10 Jan 1980, it starts from 23 December 1979.
(I don't know if that's the case but I have MMM dd YYYY in UIPickerView whereas it's dd MMM YYYY for the strings.. I don't think though because while saving, it saves the right value)..
To use correct format string is most important..
YYYY is week-based calendar year. (used in ISO week-year calendar)
yyyy is ordinary calendar year.
so, You should use 'yyyy' instead of 'YYYY'.
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "dd MMM yyyy"
print(birthday)
let date = dateFormatter.dateFromString(birthday)
self.datePicker.setDate(date!, animated: true)
For more string format for Date: refer this link
https://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx
You are using the wrong format. You need dd MMM yyyy.

Resources