2 mapmapm MapMapM 于 2012.10.22 10:50 提问

如何格式化字符串变量的数据?

NSString存日期:

1900-01-01T11:00:00

需要进行格式化,然后加了一个格式器:

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

如何进行格式化?

NSString* formatedDateString = [df ???];

3个回答

lanhailvcao
lanhailvcao   2012.10.22 11:44

你要创建两个日期格式器。一个用于解析,一个用于格式化~

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];
eagleyan
eagleyan   Rxr 2014.12.06 06:51

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];

NSDate *date = [NSDate dateWithTimeIntervalSinceReferenceDate:162000];

NSString *formattedDateString = [dateFormatter stringFromDate:date];
NSLog(@"formattedDateString: %@", formattedDateString);
// Output for locale en_US: "formattedDateString: Jan 2, 2001".

lbsaiyun
lbsaiyun   2015.02.13 16:06

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

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!