如何从stringvariables格式化数据

我有一个NSString保持date:

1900-01-01T11:00:00 

我需要格式化,所以我有一个格式化程序:

 NSDateFormatter *df = [[NSDateFormatter alloc] init]; [df setDateFormat:@"dd/MM/yyyy hh:mm:ss a"]; 

如何现在格式化这个string?

 NSString* formatedDateString = [df ???]; 

创build一个date格式化程序,它为给定的string返回一个date对象,创build第二个date格式程序,该date格式程序从该date返回所需格式的string。

 NSDateformatter *inputFormatter = [[NSDateformatter alloc] init]; [inputFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss"]; [inputFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]]; NSDate *date = [inputFormatter dateFromString:dateString]; NSDateformatter *outputFormatter = [[NSDateformatter alloc] init]; [outputFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]]; [outputFormatter setDateFormat:@"dd/MM/yyyy hh:mm:ss a"]; NSString *outputString = [outputFormatter stringFromDate:date]; 

但是,您也可以让输出date格式化程序根据区域设置来决定样式:

 NSDateformatter *outputFormatter = [[NSDateformatter alloc] init]; [outputFormatter setDateStyle:NSDateFormatterShortStyle]; [outputFormatter setTimeStyle:NSDateFormatterShortStyle]; NSString *outputString = [outputFormatter stringFromDate:date]; 

对于美国的语言环境,您现在应该使用所需的格式。


如果要为用户强制执行格式“dd / MM / yyyy hh:mm:ss a”,则还应该设置输出date格式化程序的语言环境。

一个完整的命令行示例。

 int main(int argc, const char * argv[]) { @autoreleasepool { NSString *dateString1 = @"2012-12-31T11:00:00"; NSString *dateString2 = @"2012-12-31T14:00:00"; NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init]; [inputFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]]; [inputFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss"]; NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init]; [outputFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]]; [outputFormatter setDateFormat:@"dd/MM/yyyy hh:mm:ss a"]; NSDate *date1 = [inputFormatter dateFromString:dateString1]; NSDate *date2 = [inputFormatter dateFromString:dateString2]; NSString *outputString1 = [outputFormatter stringFromDate:date1]; NSString *outputString2 = [outputFormatter stringFromDate:date2]; NSLog(@"%@", outputString1); NSLog(@"%@", outputString2); } return 0; } 

如果你不设置语言环境,像[outputFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]]; 它会导致与用户的默认语言环境做一个丑陋的混合格式,如果它不是'en_US'

即用德语: 31/12/2012 02:00:00 nachm. 这是德文不使用的格式。

为了支持用户的语言和语言环境,您应该使用不同的样式。


所提供的q&a rmaddy指出,在less数情况下,parsing格式被重写。 为了避免这种情况,请将input格式的格式设置为特定的语言环境,如[inputFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];

您需要创build两个date格式化程序,一个用于parsing,一个用于格式化; 你的parsing格式是“yyyy-MM-ddThh:mm:ss”,输出格式是“dd / MM / yyyy hh:mm:ss a”。 所以,这样的事情:

 NSString * targetString = @"1900-01-01'T'11:00:00"; NSDateFormatter *parseFormat = [[NSDateFormatter alloc] init]; [parseFormat setDateFormat:@"yyyy-MM-ddThh:mm:ss"]; NSDate * date = [parseFormat dateFromString:targetString]; NSDateFormatter * outputFormat = [[NSDateFormatter alloc] init]; [outputFormat setDateFormat:@"dd/MM/yyyy hh:mm:ss a"; NSString * outputString = [parseFormat stringFromDate:date]; 

获取当前date的示例:

 -(void) getDate { NSString *theDate; NSString *theTime; NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; [dateFormat setDateFormat:@"yyyy-MM-dd"]; NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init]; [timeFormat setDateFormat:@"HH:mm:ss"]; NSDate *now = [[NSDate alloc] init]; theDate = [dateFormat stringFromDate:now]; theTime = [timeFormat stringFromDate:now]; //2012-09-21 02:00:00 self.deviceCurrentTime=[NSString stringWithFormat:@"%@ %@",theDate,theTime]; }