EDT not working with NSDateFormatter - ios

In iOS, I am using a NSDateFormatter with the DateFormat EEE, dd MMM yyyy HH:mm:ss z.
The String Sat, 29 Aug 2015 12:34:36 EDT does not work and gives back nil when given to the function .dateFromString(). The exact same string with GMT (Sat, 29 Aug 2015 12:34:36 GMT) gives me the correct date, though.
What am I missing here?

So the problem was that the locale I was using wasn't a usual one. I live in Germany and use English as my system language, so the Locale was one with the identifier en_DE. Both de_DE and en_US work with the usual Time Zones (Like EDT), but the unusual en_DE doesn't work with all of them. So the fix was to use en_US as the locale.

Hopefully this should work, I'm in New Zealand but set locale to "EDT"
let string = "Sat, 29 Aug 2015 12:34:36 EDT"
let formatter = NSDateFormatter()
formatter.locale = NSLocale(localeIdentifier: "EDT")
formatter.dateFormat = "EEE, dd MMM yyyy hh:mm:ss z"
let localDate = formatter.dateFromString(string)

Related

DateFormatter return nil in some case [duplicate]

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

Issue in formatting date string Swift 3 [duplicate]

This question already has answers here:
NSDateFormatter doesn't show time zone abbreviation for "Asia/Kolkata" for the "z" or "zzz" specifier, just the GMT offset
(1 answer)
What is the best way to deal with the NSDateFormatter locale "feature"?
(4 answers)
Closed 5 years ago.
I need to convert the following date string in to a Date in Swift 3.
Fri Dec 09 16:18:43 AMST 2016
Here is the code that i have been using, but it's getting cash on this particular date string conversion. (This date was logged on Android using new Date().toString() method.)
static func formatDate(date: String) -> String {
let dateFormatter = DateFormatter()
dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
dateFormatter.dateFormat = "EEE MMM dd HH:mm:ss zzz yyyy"
//Works for "Fri Sep 16 10:55:48 GMT+05:30 2016"
var myDate = dateFormatter.date(from: date)
// My date returns nil on "Fri Dec 09 16:18:43 AMST 2016"
dateFormatter.dateFormat = "dd/MM/yyyy"
return "\(dateFormatter.string(from: myDate!))"
}
There are both type of strings in the database. I tried with various types of Timezone formats (z/zz/zzz/zzzz/zzzzz) but always myDate returns nil.
Any help would be appreciated.
Thanks In Advance.
Apple doc for TimeZone(abbreviation:):
In general, you are discouraged from using abbreviations except for unique instances such as “GMT”. Time Zone abbreviations are not standardized and so a given abbreviation may have multiple meanings.
Does AMST represents "Amazon Summer Time" (UTC-3) or "Armenia Summer Time" (UTC+5)? See: https://www.timeanddate.com/time/zones
That's probably why it can't detect the proper timezone to use.
Solutions I can propose:
If you know which timezone AMST is:
replace AMST by UTC-3 or UTC+5 in the date string
remove AMST from the date string and use dateFormatter.timeZone = TimeZone(secondsFromGMT: -3 or 5 * 3600)
Have your source output a more precise timezone.
Note the following code, where AMST is understood correctly:
let df = DateFormatter()
df.locale = Locale.init(identifier: "pt_BR") // assuming AMST is Amazon Summer Time (UTC -3)
df.dateFormat = "HH:mm:ss z"
print(df.date(from: "16:18:43 AMST")) // Optional(2000-01-01 19:18:43 +0000)
But as soon as you include English day or month names (e.g. Fri or Dec) it will produce nil (because they aren't in Portuguese).

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

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.

Format internet time string (Swift 1.2)

I'm trying to format a string from internet time to something more readable.
The input I have is something like: Mon, 27 Apr 2015 20:00:00 +0000
And I'd like to format it into just: Mon, 27 Apr
I'm fairly new to Swift so I don't know the best way to do this.
something like:
let unformattedDateString = "Mon, 27 Apr 2015 20:00:00 +0000"
let dateFormatter = NSDateFormatter()
// input format
dateFormatter.dateFormat = "E, dd MMM yyyy HH:mm:ss Z"
// create NSDate from String
let date = dateFormatter.dateFromString(unformattedDateString)!
// output format
dateFormatter.dateFormat = "E, dd MMM"
// create String from NSDate
let formattedDateString = dateFormatter.stringFromDate(date)

Resources