NSDateFormatterStyle.MediumStyle doesn't show in iPhone - ios

I'm using NSDateFormatterStyle.MediumStyle in my application to convert the date. I display the date on label with font size of 12
It functions on the
iPad
simulator it show like "nov 12,1994" but on my
iPhone
device it shows short formatter: "11/12/1994".
Do you know why it function on the iPad but not in the iPhone

From the NSDateFormatterStyle documentation :
The format for these date and time styles is not exact because they
depend on the locale, user preference settings, and the operating
system version. Do not use these constants if you want an exact
format.
That said, I assume your iPad and your iPhone are set to different locales
If you want your dates to be as per the iPad example above across all locales, you can create your own formatter
let date = NSDate()
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "MMM d, yyyy"
dateFormatter.stringFromDate(date)

Related

How to localised string with first later capital in swift?

I am trying to localised string.
In english i am getting like "Friday, Jun 26"
but in spanish its like "jueves, jun 25".
first letter is small. but I am trying to get just as English with first letter caps.
Bellow is my code.
let longDateFormatter = DateFormatter()
longDateFormatter.dateFormat = "EEEE, MMM d"
longDateFormatter.locale = Locale(identifier: "es")
is there any way to get date with first letter caps. Thanks for help/
Apparently Spanish does not capitalize the names of months and days of the week like we do in English. Thus the format you are getting is correct for Spanish, and you should stop trying to change it. (in essence, this is an x/y problem.)
See this link: https://www.spanishdict.com/answers/181002/do-months-need-to-be-capitalized-in-spanish#:~:text=Spanish%20does%20NOT%20capitalize%3A&text=%3F-,Calendar%3A%20Names%20of%20the%20days%20of%20the,and%20months%20of%20the%20year.&text=%3F-,Nationality%3A%20Although%20names%20of%20countries%20and%20cities%20are%20capitalized%2C%20words,derived%20from%20them%20are%20not.
If you want to do something different than the correct localization for Spanish you will need to take the output from the Spanish localization and manipulate it. You could simply use longDateFormatter.string(from: date).capitalized, which would capitalize every word in the resulting date string.
let longDateFormatter = DateFormatter()
longDateFormatter.dateFormat = "EEEE, MMM d"
longDateFormatter.locale = Locale(identifier: "es")
let output = longDateFormatter.string(from: Date()).capitalized
print(output)
Yields
Viernes, Jun 26
But again, that is the WRONG way to display dates in Spanish. It is every bit as wrong as displaying "friday, june 26" in English.

DateFormatter wrong time output when a different month is selected

I have a separate DatePicker and TimePicker component in my app.
Once the user has selected both the desired Date and Time, I construct a new Date object like this:
let timeStamp = Date(year: selectedDate.year, month: selectedDate.month, day: selectedDate.day, hour: selectedTime.hour, minute: selectedTime.minute)
I then use DateFormatter to output the exact time that the user has selected like this:
let formatter = DateFormatter()
formatter.dateFormat = "HH:mm"
formatter.string(from: timeStamp)
Now I have a very weird bug where sometimes time output will be correct (time will be displayed in UTC+2) and sometimes it'll be incorrect (time will be displayed in UTC+1) and I have absolutely no idea what could be causing this.
Example 1 (correct output):
User selects: May 26, 2020 - 18:38
Date ISO output: "2020-05-26T16:38:00Z"
DateFormatter output: "18:38"
This is the correct output
Example 2 (wrong output):
User selects: March 26, 2020 - 18:38
Date ISO output: "2020-03-26T16:38:00Z"
DateFormatter output: "17:38"
This is not the correct output. Time should be 18:38 like in the above example.
Someone please tell me how is this possible? Literally the only difference is user picked March instead of May (different month) and that for some reason confuses the DateFormatter, so Time output is in a different timezone.
I am using SwiftDate to work with dates in general.
Set correct formatter.locale, you can try Locale(identifier: "en_US_POSIX") or try to use formatter.timeZone property. Maybe TimeZone.current or TimeZone(secondsFromGMT: 0) will fix your problem
That is probably because in May daylight saving is in effect and the difference to UTC changes from +1 to +2 hours
You can use the current TimeZone and add configure your DateFormatter with it
let timezone = TimeZone.current
dateFormatter.timeZone = timezone
That should make sure that you always use the same timezone that is currently used by your device

Issue with 24hour date format

I am trying to convert Date object in string using DateFormatter but I am getting some weird behavior. I have a timestamp value (1513356296) which I want to convert in 24hour format along with date and timeZone (My current is -0800). Here is my code:
let date = Date(timeIntervalSince1970: Double(1513356296))
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
dateFormatter.timeZone = TimeZone(secondsFromGMT: -(8*3600))
dateFormatter.locale = Locale.current
I am expecting a result value of 2017-12-15T08:44:56-0800 but I am getting 2017-12-15T8:44:56-0800. Everything is same except the hour value. The hour value is 8 instead of 08.
This is not happing with all the test, it happened in only one device and rest are fine.
I also tried by checking if hour value changes if I change device time setting to 12hour format from 24hour format to see what I get for HH and it was always giving me 08 in my case. Only one user was facing this issue.
What's wrong with my code? or is there something wrong with HH I am using?

iOS System locale identifier is empty

I've noticed something weird while using date formatters. Below is the code for the date formatter.
let formatter = NSDateFormatter()
formatter.dateFormat = "EEE dd MMM h:mm a"
formatter.locale = NSLocale.systemLocale()
print(formatter.stringFromDate(NSDate()) )
The output is: "Fri 18 M03 1:05 PM". Which is kind of weird. However removing the formatter's locale gives me the output that I want: "Fri 18 Mar 1:05 PM".
I also tried printing out NSLocale.systemLocale(), and the output is an empty string. Is that normal? And what is actually happening to the date formatter when you change the locale?
FYI: I'm testing this on an actual device. And also changing the Region formats in device settings have no affect on the locale identifier.
If you want to use the system setting, should use this one NSLocale.currentLocale()
In Apple's API Document already have a point on this.
Discussion
Use the system locale when you don’t want any localizations. Use the current locale to format text that you display to users.
FYI: https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSLocale_Class/index.html#//apple_ref/occ/clm/NSLocale/systemLocale

Retrieve current date without leading zeroes

I am trying to compare the date chosen on a calendar (Kal calendar implementation) with the current date. The issue is that the current date is in MM/dd/yyyy format, whereas the Kal dates will have no leading zeroes if the month or day is below 10. Is there an easy way to retrieve the current date so that it will not have leading zeroes for the day or month if it's under 10? (The current date will actually be utilized as an attribute for saved objects, so that they can later be queried using the date selected with Kal)
example current date - 07/07/2014
example Kal date - 7/7/2014
Don't compare strings holding dates. Create actual NSDate objects, using an NSDateFormatter, and compare those. The format strings you need are "MM/dd/yyyy" for your date, however you're retrieving it, and "M/d/yyyy" for Kal's date.
I have used en_US_POSIX along with "MM/dd/yyyy" & it's Working Fine
let formatter = DateFormatter()
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.dateFormat = "MM/dd/yyyy"
https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html#//apple_ref/doc/uid/TP40002369-SW7

Resources