21

我有一个日期时间格式的字符串:“YYYY-MM-DD HH:MM:SS”。

我在我的源代码中使用它:

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

我想从“2010-01-10 13:55:15”转换为“10. January 2010”。但是我的实现不起作用。

这里有什么问题?

非常感谢提前和最好的问候。

更新的源代码:

[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

4 回答 4

33

日期格式化程序一次只能处理一种格式。你需要采取这种方法:

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 现在将是“2010 年 1 月 10 日”

于 2010-01-09T22:59:47.920 回答
6

以下是一些使用我的代码中的数据格式化程序的示例。您应该能够使用其中任何一种功能并根据您的格式对其进行调整。

用法

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

职能

+ (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;
}
于 2010-01-09T22:38:58.320 回答
2

首先,确保将行为设置为 10.4 - 更现代,根据我的经验效果更好。

[dateTimeFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];

接下来,如果它们有 2 个不同的字符串表示形式,则不能使用相同的格式进行解析和格式化,因此使用 2 个格式化程序,或者在解析和格式化之间更改字符串格式。

还要确保考虑格式选项:

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

小写的 e 是星期几,小写的 d 是月份的日期。

对于月份,使用 MMMM,而不是 B。

于 2010-01-09T22:37:30.720 回答
1

您想使用[NSFormatter dateFromString:]将基于字符串的日期转换为NSDate实例。从那里你想使用stringFromDate,而NSDate不是你上面写的字符串。我不确定是否使用相同的 NSDateFormatter 进行解析和格式化 - 您可能需要两个单独的实例来处理不同的格式样式。

于 2010-01-09T22:30:41.020 回答