How to handle different date time formats using NS

2019-01-23 03:06发布

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

4条回答
我想做一个坏孩纸
2楼-- · 2019-01-23 03:20

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"

查看更多
forever°为你锁心
3楼-- · 2019-01-23 03:33

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.

查看更多
爷的心禁止访问
4楼-- · 2019-01-23 03:39

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.

查看更多
神经病院院长
5楼-- · 2019-01-23 03:44

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;
}
查看更多
登录 后发表回答