Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle different date time formats using NSDateFormatter

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];
like image 282
Tim Avatar asked Jan 09 '10 22:01

Tim


3 Answers

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"

like image 92
Niels Castle Avatar answered Nov 12 '22 00:11

Niels Castle


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;
}
like image 36
Andrew Johnson Avatar answered Nov 11 '22 23:11

Andrew Johnson


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.

like image 2
Andrew Kuklewicz Avatar answered Nov 12 '22 01:11

Andrew Kuklewicz