I have a date that I would like to format depending on what country the user is from.
The date: 10th of March 2015.
Let's say the user is from USA, I'd like: 03/10/15
Let's say the user is from France, I'd like 10/03/15
I guess I could check if the locale is like en-US and fr-FR and format it accordingly but is there a way to make it automatic and for all locales?
You can use the defaultTimeZone of NSDateFormatter()
var dateFmt = NSDateFormatter()
dateFmt.timeZone = NSTimeZone.defaultTimeZone()
You can use one of the NSDateFormatter styles:
NSDateFormatterNoStyle
NSDateFormatterShortStyle
NSDateFormatterMediumStyle
NSDateFormatterLongStyle
NSDateFormatterFullStyle
Or if you already have a format string in mind (like "MM-DD-YYYY") you can pass it through [NSDateFormatter dateFormatFromTemplate:options:locale:];
Related
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.
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
I have problem with my DateFormatter.
My iOS app communicates with server and uses If-Modified-Since header with date created with following formatter:
modifiedSinceDateFormatter = DateFormatter()
modifiedSinceDateFormatter!.dateFormat = "EEE, dd MMM yyyy HH:mm:ss zzz"
modifiedSinceDateFormatter!.locale = Locale(identifier: "en_US")
modifiedSinceDateFormatter!.timeZone = TimeZone(abbreviation: "GMT")
It works as expected - returning date in following format: Fri, 08 Sep 2017 07:02:20 GMT.
But I was looking through the server logs and found that once request was made with following date format sob., 26 sie 2017 10:17:01 CEST (that's correct Polish locale and timezone - I expect my users to use Polish locale).
So my question is:
How is it possible that this formatter returned date in the wrong locale? Are there some options that user can activate to override this locale (like Accessibility options)? Can it be some jailbroken device?
EDIT: And it happened again: wt., 17 kwi 2018 08:40:02 CEST. Interesting that there was few requests (at same moment from single device) and only one of them failed - with wrong date).
I found the following explanation on how to ensure you get your date properly parsed using English names for months and days
Unless you specifically need month and/or weekday names to appear in the user's language, you should always use the special locale of en_US_POSIX. This will ensure your fixed format is actually fully honored and no user settings override your format. This also ensures month and weekday names appear in English. Without using this special locale, you may get 24-hour format even if you specify 12-hour (or visa-versa). And dates sent to a server almost always need to be in English.
I found the quote on this page
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
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