My current code is
NSNumberFormatter *f = [[[NSNumberFormatter alloc] init] autorelease];
[f setNumberStyle:NSNumberFormatterCurrencyStyle];
[f setCurrencySymbol:NSLocalizedString(#"CURRENCY", #"Get Currency")];
NSString * stringCurrecy = [f stringFromNumber:(-70.00)];
I'm using NSLog to check the string currency and it's printing "($ 70.00)".
I changed "(", ")" symbol. How can I achieve this:
( $ 70.00 ) -> - $70.00 or $ -70.00
You have to set the negative format.
Add this line:
[f setNegativeFormat:#"-¤#,##0.00"];
but maybe you just want to set the locale with [f setLocale:] instead of setting every format option on your own.
(and next time post code that does compile.)
I have a NSNumber category that does this:
#implementation NSNumber (Formatter)
- (NSString *)currencyStringValue
{
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
formatter.locale = [NSLocale currentLocale];
formatter.numberStyle = NSNumberFormatterCurrencyStyle;
formatter.negativePrefix = [NSString stringWithFormat:#"- %#", formatter.currencySymbol];
formatter.negativeSuffix = #"";
return [formatter stringFromNumber:self];
}
#end
Setting negativeFormat to "-" almost worked for me, but it was dropping off the currency symbol. At least for en_US locale, this fits my needs:
NSLocale *US = [NSLocale localeWithLocaleIdentifier:#"en_US"];
NSNumberFormatter *currencyFormatter = [[NSNumberFormatter alloc] init];
[currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[currencyFormatter setCurrencyCode:currencyCode];
[currencyFormatter setLocale:priceLocale];
//manually add - prefix to positive format...
NSString *negFormat = [NSString stringWithFormat:#"-%#",currencyFormatter.positiveFormat];
[currencyFormatter setNegativeFormat:negFormat];
I'm not sure whether this is appropriate for all locales, but it works for en_US.
Related
i have a NSdatepicker and when i change the year, it changes another NSTextFile´s value to the number of years till now like this:
- ( IBAction)setExp:(id)sender {
NSDateFormatter *df = [NSDateFormatter new];
[df setDateFormat:#"yyyy"];
NSString *TodayString = [df stringFromDate:[NSDate date]];
NSString *TargetDateString = [df stringFromDate: startedPicker.dateValue ];
NSTimeInterval time = [[df dateFromString:TodayString] timeIntervalSinceDate:[df dateFromString:TargetDateString]];
int days = time /60 /60 / 24 /365;
NSString *intString = [NSString stringWithFormat:#"%d", days];
[expLbn setStringValue:intString];
}
but the new data from this action docent get stored in the core data for the NSTextField, if i type in a number in the same textfield the data gets stored.
i tryed http://www.red-sweater.com/blog/229/stay-responsive but can't make that work.
I'm trying to produce a string using a NSDate category in this way:
NSString* dateString = nil;
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setLocale:[NSLocal currentLocale]];
[dateFormat setDateFormat:#"dd LLL YYYY"];
dateString = [dateFormat stringFromDate:self];
return dateString;
The conversion works fine except in ONE case (I report the debug session):
if I try to convert an NSDate object like this:
(gdb) po self
2012-01-01 00:00:00 +0000
I obtain:
(gdb) po dateString
01 Jan 2011
Why the year is set back to 2011????
PS. I have already checked NSDate returns wrong year and I'm NOT using the Japanese calendar.
thanks a lot
Try this:
NSDate *pickerDate = [NSDate date];
NSCalendar* calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDateComponents* components = [[[NSDateComponents alloc] init] autorelease];
components.day = 0; //This value to take from today to next 1 or 2 or 3 days
NSDate* newDate = [calendar dateByAddingComponents: components toDate: pickerDate options: 0];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MMMM"];
NSString *textDate = [NSString stringWithFormat:#"%#",[dateFormatter stringFromDate:newDate]];
[dateFormatter release];
This extremely simple task was apparantly very hard...
NSDate *date;
date = [someMethod here];
//I've checked with debugger that the method returns an object of type NSDate.
//The description of date at this point is: "2012-02-02 19:42:00 +0000"
NSDateFormatter *dateFormat;
[dateFormat setDateFormat:#"dd-MM-yy hh:mm"];
NSString *dateString = [dateFormat stringFromDate:date];
dateString is just NIL
Does anyone know what's wrong?
Edit: What i really want to achieve is simply:
NSString *receivedDate = #"2012-02-02T20:42:00+01:00";
NSString *fixedDate = [do some magic]
//value of fixedDate is now: "02-02-12 20:42"
As Anna said you need to allocate an instance of NSDateFormatter because all you get is a NULL ptr, which just ignores the messages setDateFormat and stringFromDate, leaving you with NULL.
But also your format is for hours is incorrect. Refer to Date Formatter reference
This works for me:
NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
[df setDateFormat:##"dd-MM-yy HH:mm"];
NSString *dateString = [df stringFromDate:date];
I have a JSON feed coming into my app, one of the fields is a combined date & time string which I need to split into discrete date and time strings for display in a table cell. An example of input from the JSON is:
2012-01-18 14:18:00.
I'm getting a bit confused with the date formatter, and clearly I'm not doing it right - I've tried a number of tutorials but most just seem to show how to format a date.
I've tried something a little like this to get just the time:
NSDictionary *rowData = [self.raceData objectAtIndex:[indexPath row]];
NSDateFormatter *dateFormat = [[[NSDateFormatter alloc] init] autorelease];
[dateFormat setDateFormat:#"h:mma"];
NSDate *raceDate = [dateFormat dateFromString:[rowData valueForKey:#"race_time"]];
NSString *raceTime = [dateFormat stringFromDate:raceDate];
but on output raceTime is just null.
Any help appreciated.
maybe the format should be more like
[dateFormatter setDateFormat:#"yyyy-MM-dd 'at' HH:mm"];
have a look at http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html
might clear things up abit
Right, I have this working - it's probably a bit messy but here's what I did:
NSDictionary *rowData = [self.raceData objectAtIndex:[indexPath row]];
NSDateFormatter *dateFormat = [[[NSDateFormatter alloc] init] autorelease];
[dateFormat setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate* raceDate = nil;
NSError* dateError = nil;
NSRange dateRange = NSMakeRange(0, [[rowData valueForKey:#"race_time"] length]);
[dateFormat getObjectValue:&raceDate forString:[rowData valueForKey:#"race_time"] range:&dateRange error:&dateError];
[dateFormat setDateFormat:#"HH.mm"];
NSString *raceTime = [dateFormat stringFromDate:raceDate];
I can now output raceTime as a standalone time. I had to use getObjectValue:forString:range:error: to parse the original string to a date before changing the formatting and parsing it again.
As I'm using this in a table I suspect I'll need to use a static formatter so it doesn't slow everything down - if anyone can give a best practice on doing that I'd appreciate it.
If you are sure that the input string format wouldn't change – you might use something similar to:
NSString *date = nil;
NSString *time = nil;
NSDictionary *rowData = [self.raceData objectAtIndex:[indexPath row]];
NSString *raceTime = [rowData valueForKey:#"race_time"];
NSArray *dateParts = [raceTime componentsSeparatedByString:#" "];
if ([dateParts count] == 2) {
date = [dateParts objectAtIndex:0];
time = [dateParts objectAtIndex:1];
}
I'm having trouble converting an ISO 8601 timestamp into an NSDate. I tried to use NSDateFormatter, but I can't get it to work with the UTC time offset that appears on the end of the timestamps. To explain, I would like to convert a timestamp such as the following into an NSDate: 2011-03-03T06:00:00-06:00. My question is: How do I deal with the "-06:00" part? I tried using yyyy-MM-dd'T'HH:mm:ssZ as my date format string but it doesn't work. Any suggestions?
No need to remove the :'s. To handle the "00:00" style timezone, you just need "ZZZZ":
Swift
let dateString = "2014-07-06T07:59:00Z"
let dateFormatter = NSDateFormatter()
dateFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZ"
dateFormatter.dateFromString(dateString)
Objective-C
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
dateFormat.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
NSString *input = #"2013-05-08T19:03:53+00:00";
[dateFormat setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZZZZ"]; //iso 8601 format
NSDate *output = [dateFormat dateFromString:input];
NSLog(#"Date output: %#", output);
In my case I received something like that:
"2015-05-07T16:16:47.054403Z"
And I had to use:
"yyyy'-'MM'-'dd'T'HH':'mm':'ss.SSSZ"
The problem is the : character inside the timezone offset. You could explicitly remove just that colon, or remove all of the colons, and then proceed. For example:
NSString *s = #"2011-03-03T06:00:00-06:00";
s = [s stringByReplacingOccurrencesOfString:#":" withString:#""];
NSDateFormatter *f = [[NSDateFormatter alloc] init];
[f setDateFormat:#"yyyy-MM-dd'T'HHmmssZZ"];
NSDate *d = [f dateFromString:s];
NSLog(#"%#", d);
This logs:
EmptyFoundation[5088:707] 2011-03-03 12:00:00 +0000
Here is the method that I use:
-(NSDate *)dateFromISO8601String:(NSString *)dateString{
if (!dateString) return nil;
if ([dateString hasSuffix:#"Z"]) {
dateString = [[dateString substringToIndex:(dateString.length-1)] stringByAppendingString:#"-0000"];
}
dateString = [dateString stringByReplacingOccurrencesOfString:#":" withString:#""];
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
dateFormatter.dateFormat = #"yyyy-MM-dd'T'HHmmssZ";
return [dateFormatter dateFromString:dateString];
}
Converting ISO 8601 timestamp into NSDate In Swift:
let dateFormatter = NSDateFormatter()
let inputDate = "2015-06-18T19:00:53-07:00"
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZ" //iso 8601
let outputDate = dateFormatter.dateFromString(inputDate)
println(outputDate!) //optional implicitly
Or optional explicitly:
if let outputDate = dateFormatter.dateFromString(inputDate) {
println(outputDate) //no need !
}
Apple supports a separate format for ISO 8601 format
Objective C
NSISO8601DateFormatter *formater = [[NSISO8601DateFormatter alloc]init];
NSString *string = [formater stringFromDate:[NSDate date]];