views:

1445

answers:

4

Hello!

I have a String with a datetime format: "YYYY-MM-DD HH:MM:SS".

I use this in my source code:

NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:@"%e. %B %Y"];
NSString *test = [formatter stringFromDate:@"2010-01-10 13:55:15"];

I want to convert from "2010-01-10 13:55:15" to "10. January 2010". But my implementation does not work.

What's wrong here?

Thanks a lot in advance & Best Regards.

Updated source code:

[NSDateFormatter setDefaultFormatterBehavior:NSDateFormatterBehavior10_4];
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:@"%Y-%m-%d %H:%M:%S"];
NSString *test1 = [formatter stringFromDate:@"2010-01-10 13:55:15"];

NSDateFormatter *formatter1 = [[[NSDateFormatter alloc] init] autorelease];
[formatter1 setDateFormat:@"%d. %M4 %Y"];
NSString *test2 = [formatter1 stringFromDate:test1];
A: 

You want to use [NSFormatter dateFromString:] to convert your string-based date to an NSDate instance. From there you want to use stringFromDate with the NSDate, not the string as you have written above. I'm not sure about using the same NSDateFormatter for both parsing and formatting - you may need two separate instances to handle the different format styles.

fbrereto
You mean NSDateFormatter? I think these methods are not available in iPhone only Mac OS X. You also link to the Mac OS X reference library.
Tim
The routines I reference are available for the iPhone, too. Glad you found your answer all the same.
fbrereto
+1  A: 

First off, make sure you set the behavior to 10.4 - more modern, works better in my experience.

[dateTimeFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];

Next, you can't use the same format to parse and format if they have 2 different string representations, so use 2 formatters, or change the string format between parsing and then formatting.

Also make sure you consider the formatting options:

http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html#//apple_ref/doc/uid/TP40002369-SW1

lowercase is e is the day of week, lowercase d is the day of the month.

For month, use MMMM, not B.

Andrew Kuklewicz
Hm, I think I do not understand the "system". I updated my first post. I do not know why this won't work.
Tim
Had a typo - by M4, I meant 4 Ms, as in MMMM - whoops.
Andrew Kuklewicz
setFormatterBehavior: isn't required on iPhone OS, it *only* operates in the 10.4+ mode.
benzado
good call - but all the more reason he should be using the 10.4 mode formatting syntax.
Andrew Kuklewicz
+2  A: 

Here are a few examples of working with data formatters from my code. You should be able to take any one of these functions and tweak it for your format.

USAGE

NSDate *today = [NSDate date];
NSDateFormatter *dateFormatter = [Constants getTitleDateFormatter];
NSString *dateString = [dateFormatter stringFromDate:today];        
[dateFormatter release];

FUNCTIONS

+ (NSDateFormatter *) getDateFormatterWithTimeZone {
  //Returns the following information in the format of the locale:
  //YYYY-MM-dd HH:mm:ss z (Z is time zone)
  NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  [dateFormatter setLocale:[NSLocale currentLocale]];
  [dateFormatter setDateStyle:NSDateFormatterShortStyle];
  [dateFormatter setTimeStyle:NSDateFormatterLongStyle];
  return dateFormatter;

}

+ (NSDateFormatter *)dateFormatterWithoutYear {
  NSDateFormatter *dateFormatter = [Constants getDateFormatterWithTimeZone];
  [dateFormatter setTimeStyle:NSDateFormatterShortStyle];
  NSString *format = [dateFormatter dateFormat];
  format = [format stringByReplacingOccurrencesOfString:@"/yy" withString:@""];
  NSRange secondSpace;
  secondSpace.location = format.length-2;
  secondSpace.length = 1;
  format = [format stringByReplacingCharactersInRange:secondSpace withString:@""];
  [dateFormatter setDateFormat:format];
  return dateFormatter;
}

+ (NSDateFormatter *) dateFormatterMonthDayOnly {
    NSDateFormatter *dateFormatter = [Constants getDateFormatterWithTimeZone];
    [dateFormatter setTimeStyle:NSDateFormatterShortStyle];
    NSString *format = [dateFormatter dateFormat];
    format = [format stringByReplacingOccurrencesOfString:@"/yy" withString:@""];
    NSRange range;
    range.location = 0;
    range.length = 3;
    format = [format substringWithRange:range];
    [dateFormatter setDateFormat:format];
    return dateFormatter;
}

+ (NSDateFormatter *) getTitleDateFormatter {
    //Returns the following information in the format of the locale:
    //MM-dd-yyyy hh:mm:ssa
    NSDateFormatter *dateFormatter = [Constants getDateFormatterWithTimeZone];
    [dateFormatter setTimeStyle:NSDateFormatterMediumStyle];
    NSString *format = [dateFormatter dateFormat];
    NSRange secondSpace;
    secondSpace.location = format.length-2;
    secondSpace.length = 1;
    format = [format stringByReplacingOccurrencesOfString:@"/" withString:@"-"];
    format = [format stringByReplacingCharactersInRange:secondSpace withString:@""];
    [dateFormatter setDateFormat:format];   
    return dateFormatter;
}
Andrew Johnson
+1  A: 

A date formatter can only handle one format at a time. You need to take this approach:

NSDateFormatter *f = [[NSDateFormatter alloc] init];
[f setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *date = [f dateFromString:@"2010-01-10 13:55:15"];

NSDateFormatter *f2 = [[NSDateFormatter alloc] init];
[f2 setDateFormat:@"d. MMMM YYYY"];
NSString *s = [f2 stringFromDate:date];

s will now be "10. January 2010"

Niels Castle