Converting String to NSDate Giving Wrong Date - ios

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.

Related

Swift date formatter ignoring months

I wrote a date reformatter but it appears Swift's date formatter itself is ignoring the months. The documentation says this shouldn't be happening. How do I make it not ignore months?
let testDate:String = "2020-11-22-11:00"
print("start date: ",testDate," reformatted date: ", reformatDate(dateString: testDate))
func reformatDate(dateString: String) -> String? {
print("dateString: ",dateString)
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-DD-HH:mm"
dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
return dateFormatter.string(from: dateString)
}
this prints:
start date: 2020-11-22-11:00 converted date: 22-01-2020 11:00 AM
It unreasonably turns all months to 1!
Your format string is incorrect. It should be:
yyyy-MM-dd-HH:mm
and
dd-MM-yyyy h:mm a
dd means day-of-month, whereas DD means day-of-year.
Note that you should also do:
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
whenever you are using a custom date format.
Parsing 2020-11-22-11:00 with yyyy-MM-DD-HH:mm means that you want the twenty second day of the year 2020, in the month November. That makes no sense, and DateFormatter ends up ignoring the month because apparently day-of-year is a "stronger" date component. The 22nd day of any year is the 22nd of January.
Then, when you format the parsed date with DD-MM-yyyy h:mm a, the month component gets displayed as 01, and the day-of-year is still displayed as 22.
Here are some useful links to learn about format specifiers, you'll just how much lowercase/uppercase matters.
NSDateFormatter.com
TR-35

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

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

Resources