How to localised string with first later capital in swift? - ios

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.

Related

How to know Date format getting from sever [duplicate]

This question already has answers here:
How can I parse / create a date time stamp formatted with fractional seconds UTC timezone (ISO 8601, RFC 3339) in Swift?
(13 answers)
Closed 1 year ago.
I am trying to get date format of a date that I am getting from server.
What will be the format of this date:
2021-10-14T17:53:03.753588+05:30
What I tried:
yyyy-MM-dd'T'HH:mm:ssXXX
But Its not working.
How to (my tips & steps):
When you are struggling to find the date format for String -> Date ask you this: What my format is really doing? What's it's parsing/interpreting? Just let's see with Date -> String...
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssXXX"
print(formatter.string(from: Date()))
Output: 2021-10-14T13:06:38+02:00: Is it the "same" as the string we have? No, some are the same, but not all...
Let's continue with another tip:
Let's put our format and the string one above the other one:
2021-10-14T17:53:03.753588+05:30
yyyy-MM-dd'T'HH:mm:ssXXX
Then, let's add "spaces", to make each pattern match its corresponding input:
2021-10-14 T 17:53:03.753588 +05:30
yyyy-MM-dd 'T' HH:mm:ss XXX
Then, let's check the doc (it's bookmarked in my web browser) for interpretation of the pattern and check if they correspond if needed, and to find the missing one if needed too.
Ok, so we aren't interpreting .753588 at all, that's why it's failing...
It's for the fractional seconds, so if we change the format to: "yyyy-MM-dd'T'HH:mm:ss.SSSXXX", it should work. Note, you can replace XXX with Z if you want...
Now, remember that patterns are case sensitive, so if you have strange hours, minutes, or nil because of that, check if you didn't misuse minutes vs month, 12h format vs 24 hour format...
If you have hours diff (or usually 30min diff), the issue could be then a timezone issue.
If you have a day diff, it could also be a timezone issue (interpret it as hours diffs around midnight, so there is a day change).
If you have a year diff, check if you didn't misuse yyyy vs YYYY.
Etc. But that should cover most of your cases (basic issues).
Try this format.
"yyyy-MM-dd'T'HH:mm:ss.SSSSSSXXX"
As the date format you mentioned in your question contains milliseconds as well as timezone offset info.
Once date is parsed then based on your need you can set output date format and will get formatted date.
let sampleDate = "2021-10-14T17:53:03.753588+05:30"
let dateFormatter = DateFormatter() dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSSSSXXX" // Set input date format.
let inputDate = dateFormatter.date(from: sampleDate)
print("Input Date:- \(inputDate)")
dateFormatter.dateFormat = "yyyy-MM-dd hh:mm:ss" // Set output date format as per need
let outputDate = dateFormatter.string(from: inputDate ?? Date())
print("Output Date:- \(outputDate)")

Incorrect locale from dateFormatter

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

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

How to format a date depending on the locale used?

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:];

How to automatically format NOW() in to a string that that displays YYYY-MM-DD?

Is it possible to format the output of NOW() to a string that displays YYYY-MM-DD?
This is the output of NOW(): 29/02/2012 12.07.37
The reason is, that I need to use the current date in a QUERY.
QUERY only accepts date in the format YYYY-MM-DD . I can't get a date directly from a cell, because it gets formatted as (even if I change the formatting): DD/MM/YYYY
Perhaps some regular expression?
If this is supposed to be an in-cell formula then you can use
=TEXT(NOW(),"yyyy-mm-dd")
I will follow JMax's suggestion and convert my comment to an answer.
Now() returns the current date and time as a number. The integer part gives the date and the fraction part gives the time. If you print or display that date, the default is to give the full date in what Microsoft think's is the local format.
Format(expn, fmt) allows you to convert an expression to a string. For example:
Dim DateStg as String
DateStg = Format(Now(),"yyyy-mm-dd")
fmt defines the format to which the expn is to be converted. fmt is a mixture of code letters (such as: "yyyy", "mm", "dd") and punctuation (such as "-"). "yyyy-mm-dd" appears to meet your current needs but you can also usethe following to format dates:
"mmm" to give three letter month (Jan, Feb, etc.)
"mmmm" to give full name of month (January, February, etc)
"ddd" to give three letter day of week (Mon, Tue, etc)
"dddd" to give full name of day of week (Monday, Tuesday, etc)
In VB.net you can do the following:
Dim dateStr As String = Now().ToString("yyyy-MM-dd")
In C# you can do it like this:
String dateStr = DateTime.Now.ToString("yyyy-MM-dd");

Resources