代碼:
NSString * dateStr = @"7/25/2013 12:00:00 AM";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"MM/dd/yyyy hh:mm:ss a"];
NSDate *date = [dateFormat dateFromString:dateStr];
NSLog(@"dateStr : %@ , date : %@",dateStr,date);
但是轉換的日期會比實際日期差一天:
dateStr : 7/25/2013 12:00:00 AM , date : 2013-07-24 18:30:00 +0000
NSString * dateStr = @"7/25/2013 12:00:00 AM";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"MM/dd/yyyy hh:mm:ss a"];
[dateFormat setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSDate *date = [dateFormat dateFromString:dateStr];
NSLog(@"dateStr : %@ , date : %@",dateStr,date);
試試看