在獲取特定時期時得到空字符。
比如13/03/2013
。
代碼:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"MM/dd/yyyy HH:mm:ss a"];
NSDate *myDate = [formatter dateFromString:@"12/03/2013 5:47:00 AM"];
NSDate *myDate1 = [formatter dateFromString:@"13/03/2013 6:25:48 AM"];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
[df setDateFormat:@"yyyy-MM-dd HH:mm"];
NSString *datestr = [df stringFromDate:myDate];
NSString *datestr1 = [df stringFromDate:myDate1];
NSLog(@"date object %@,%@",datestr,datestr1);
需要看一下標准日期格式:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"dd/MM/yyyy hh:mm:ss a"];
NSDate *myDate = [formatter dateFromString:@"12/03/2013 5:47:00 AM"];
NSDate *myDate1 = [formatter dateFromString:@"13/03/2013 6:25:48 AM"];
[formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm"];
NSString *datestr = [formatter stringFromDate:myDate];
NSString *datestr1 = [formatter stringFromDate:myDate1];
NSLog(@"date object %@,%@",datestr,datestr1);