I'm implementing requirements from PO to show date and time in the application. We are targeting to NL, DE and UK market (with possible expansion further in the nearest future).
Here is my code:
formatter = [[NSDateFormatter alloc] init];
[formatter setCalendar:[NSCalendar currentCalendar]];
[formatter setDateStyle:NSDateFormatterFullStyle];
[formatter setTimeStyle:NSDateFormatterNoStyle];
The problem that I faced - iOS NSDateFormatter returns date starting with capital first letter for the Dutch locale - "Woensdag 25 februari 2015 15:42". As requirement we should have something "woensdag 25 februari 2015 15:42". Of course I could "hack" it and add check for Dutch locale to lowercase the first letter. The questions are:
Are there guidelines about date notation for Dutch locale?
Is there better way to solve issue with standard API?
In iOS8 Apple introduced the enum NSFormattingContext.
If you need it lower case because you want to put the date into a sentence that should be exactly what you want.
formatter.formattingContext = NSFormattingContextMiddleOfSentence;
Full list of options:
enum {
NSFormattingContextUnknown = 0,
NSFormattingContextDynamic = 1,
NSFormattingContextStandalone = 2,
NSFormattingContextListItem = 3,
NSFormattingContextBeginningOfSentence = 4,
NSFormattingContextMiddleOfSentence = 5,
};
Related
Hello I am getting the starting day number of the week in this way
NSDateFormatter *formatter=[[NSDateFormatter alloc]init];
[formatter setDateFormat:#"c"];
NSString *dayofweek=[formatter stringFromDate:startdate];
This strdate is a like 11/01/2016.
Then I convert it into a intand my button width is 41 so I take the X position where should I place this starting date button in this way.
int wday=[dayofweek intValue];
posx=41*wday;
My days name arranged like this.
S M T W TH F S
My problem is when I change my phone region to Austrailia it takes this wday as 3. But If I changed the region to an Asian country wday takes as 2.
How can I solve this issue?
Please help me.
Thanks
To get a locale-independent weekday use:
NSInteger weekday = [[NSCalendar autoupdatingCurrentCalendar]
components:NSCalendarUnitWeekday
fromDate:startDate].weekday;
By default, the date picker shows 1 - January - 2014.
Can user pick as
1 - 1 - 2014 ?
How? Thanks!
You can't change the date picker to that format. Apple provides you the following datepicker modes, each of them has its own format:
UIDatePickerModeTime
The date picker displays hours, minutes, and (optionally) an AM/PM designation. The exact items shown and their order depend upon the locale set. An example of this mode is [ 6 | 53 | PM ].
UIDatePickerModeDate
The date picker displays months, days of the month, and years. The exact order of
these items depends on the locale setting. An example of this mode is
[ November | 15 | 2007 ].
UIDatePickerModeDateAndTime
The date picker displays dates (as unified day of the week, month, and day of
the month values) plus hours, minutes, and (optionally) an AM/PM
designation. The exact order and format of these items depends on the
locale set. An example of this mode is [ Wed Nov 15 | 6 | 53 | PM ].
UIDatePickerModeCountDownTimer
The date picker displays hour and minute values, for example [ 1 | 53 ].
The application must set a timer to fire at the proper interval and
set the date picker as the seconds tick down.
If you want any other format, you must implement your own datePicker.
#Luis is right, if you want the month in numbers you can get. below code will not change how UIDatePicker shows month. but will change your selected month in number. and you can pick as 1 - 1 - 2014.
NSDateFormatter *date = [[NSDateFormatter alloc]init];
[date setDateFormat:#"dd-M-YYYY"];
YourDatepicker.datePickerMode = UIDatePickerModeDate;
NSString *date1 = [date stringFromDate:YourDatepicker.date];
date1 will give you the 1 - 1 - 2014.
func datePickerValueChanged(sender:UIDatePicker) {
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "dd-MMM-yyyy"
let dateString = dateFormatter.stringFromDate(sender.date)
departureDateTextField.text = dateString
}
My application is allowing user to select various currencies for spendings tracking.
I have a label which displays the amount with currecy symbol. I'm using NSNumberFormatter with kCFNumberFormatterCurrencyStyle to format the amount string and display it in the label;
numberFormatter = [[NSNumberFormatter alloc] init];
numberFormatter.numberStyle = kCFNumberFormatterCurrencyStyle;
numberFormatter.currencyCode = #"EUR";
My goal is to display the currency symbol with different color, so i'm using NSAttributedString, trying to find symbols' range and set different attributes to it. The problem is that the formatter return wrong symbol when I initilizing the attributed string:
MLOG(#"internationalCurrencySymbol %#", numberFormatter.internationalCurrencySymbol);
MLOG(#"currencySymbol %#", numberFormatter.currencySymbol);
MLOG(#"currencyCode %#", numberFormatter.currencyCode);
//logs:
//USD
//$
//EUR
but when the label is displayed on the screen I see correct Euro currency symbol: €
Does anybody know how can get the currency symbol for given currency code?
I just tested your code. If you run the log statements right after defining the number formatter the way you did, the output is
#"EUR"
#"€"
#"EUR"
Note that international currency symbol and currency symbol seem to depend on the locale set in your system. But you can easily change the locale for the formatter like this:
numberFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"de_DE"];
It would probably be best to not set the currencySymbol at all. Then when the locale is, say, Thailand (#"th_TH"), you get
numberFormatter.internationalCurrencySymbol "THB"
numberFormatter.currencySymbol "฿"
numberFormatter.currencyCode "THB"
This is an issue that I have been having for quite some time, and I cannot for the life of me get it fixed properly. Using the RSS Feed http://www.localendar.com/public/RogerJohnson?style=M3,
I have my app set up to parse the XML and list each calendar entry into the row of a TableView. The issue pops up when DST begins. Localendar doesn't have a feature for checking if an event occurs in DST, so in the RSS feed the pubDate always shows as:
Fri, 02 May 2014 19:00:00 EST
The event for that day actually begins at 7:00 PM EDT. So, when the app converts everything around, it takes the EST to heart, and knowing it is currently in DST, displays the time as 20:00:00 EDT. How can I properly set this NSDate so that no matter if it is DST or not, it will show 7:00 instead of adjusting by an hour?
Here is how I am parsing the RSS:
NSString *articleDateString = [item valueForChild:#"pubDate"];
NSDate *articleDate = [NSDate dateFromInternetDateTimeString:articleDateString formatHint:DateFormatHintRFC822];
NSLog(#"%#", articleDate);
RSSEntryCalendar *entry = [[[RSSEntryCalendar alloc] initWithBlogTitle:blogTitle articleTitle:articleTitle articleUrl:articleUrl articleDate:articleDate articleImage:bodyoftext] autorelease];
Then, displaying in table view I have for cellForRowAtIndexPath:
RSSEntryCalendar *entry = [_allEntries objectAtIndex:indexPath.row];
NSDateFormatter * dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setTimeStyle:NSDateFormatterMediumStyle];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"America/New_York"]];
NSString *articleDateString = [dateFormatter stringFromDate:entry.articleDate];
cell.detailTextLabel.text = articleDateString;
As mentioned before, pubDate always shows EST, regardless of DST being in effect or not. If it shows 19:00:00 it should be 7, regardless of time of year, but is constantly changing during DST.
I put in a NSLog for the NSDate where I parse it, and here is what I get for one entry:
2014-05-03 00:00:00 +0000
The time that shows up in the table view is May 2nd, 2014 8:00PM, while the actual event occurs at May 2nd, 2014 7:00PM. Hopefully this helps to demonstrate issue.
Per the RSS 2.0 Specification:
All date-times in RSS conform to the Date and Time Specification of RFC 822, with the exception that the year may be expressed with two characters or four characters (four preferred).
Per RFC 822:
zone = "UT" / "GMT" ; Universal Time
; North American : UT
/ "EST" / "EDT" ; Eastern: - 5/ - 4
/ "CST" / "CDT" ; Central: - 6/ - 5
/ "MST" / "MDT" ; Mountain: - 7/ - 6
/ "PST" / "PDT" ; Pacific: - 8/ - 7
/ 1ALPHA ; Military: Z = UT;
; A:-1; (J not used)
; M:-12; N:+1; Y:+12
/ ( ("+" / "-") 4DIGIT ) ; Local differential
; hours+min. (HHMM)
As you can see RFC 822 dates have a very limited set of time zone abbreviations - but they are in the spec and they have very specific meaning. "EST" = UTC-5.
If the service you're using (Localendar) is erroneously sending back EST when they mean EDT - that's a bug you should take up with them. They are not properly adhering to the RSS spec.
Working around the bug in your own code is possible - but it's not the best idea:
One day they might fix it, and then your code could be broken.
Local times can be ambiguous during the fall-back daylight saving time transition. A value like "Sun, 2 Nov 2014 01:30:00 EST" should have a singular meaning, separate from "Sun, 2 Nov 2014 01:30:00 EDT". By assuming EST=ET, then you have no way to tell these two points apart.
My objective is to create a customer calculator application for iPhone and I am using Xcode to write my application. My problem, that I cannot find a solution for, is how to format a number that uses decimals (with extra zeros) without switching into scientific notation
I tried...
buttonScreen.text = [NSString stringWithFormat:#"%0.f",currentNumber];
%0.f formatting always rounds so if the user types in "4.23" it displays "4"
%f formats numbers with 6 decimals (typing in '5' displays as '5.000000'), but I don't want to show extra zeros on the end of the number.
%10.4f is something else that I have seen in my reading to find the solution, but my problem is that I don't know how many decimals will be in the answer, and I may want zero decimals or 10 decimals depending on the number.
The following are examples of numbers I'd like to display (without the commas): A whole number larger than 6 digits, a decimal number with more than 6 digits.
123,456,789;
0.123456789;
12345.6789;
-123,456,789;
-0.23456789;
-12345.6789;
*This is a spiritual repost to my earlier question "How to Format Numbers without scientific notation or decimals" which I poorly phrased as I intended to write 'unnecessary (extra zeros),' but upon rereading my post clearly witnessed my inability to convey that at any point in my question.
Use the NSNumberFormatter class.
First define the formatter:
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
Then you can define various properties of the formatter:
formatter.numberStyle = NSNumberFormatterDecimalStyle;
formatter.maximumIntigerDigits = 3;
formatter.minimumFractionDigits = 3;
formatter.maximumFractionDigits = 8;
formatter.usesSignificantDigits = NO;
formatter.usesGroupingSeparator = YES;
formatter.groupingSeparator = #",";
formatter.decimalSeparator = #".";
....
You format the number into a string like this:
NSString *formattedNumber = [formatter stringFromNumber:num];
Play around with it. Its pretty simple, but may take some work to get the look you would like.
Actually, it makes more sense to use this:
label.text = [NSString stringWithFormat:#"%.4f", answer];
This tells XCode to display your number with 4 decimal places, but it doesn't try to "pad" the front of the number with spaces. For example:
1.23 -> " 1.2300" // When using [NSString stringWithFormat:#"%9.4f", answer];
1.23 -> "1.2300" // When using [NSString stringWithFormat:#"%.4f", answer];
try something like this
label.text = [NSString stringWithFormat:#"%9.4f", answer];
where the 9 means total digits (in terms of padding for alignment), and the 4 means 4 decimal places.