本文转载至 http://blog.csdn.net/ios_che/article/details/8090977
- 设置时间显示格式:
- NSString* timeStr = @"2011-01-26 17:40:50";
- NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
- [formatter setDateStyle:NSDateFormatterMediumStyle];
- [formatter setTimeStyle:NSDateFormatterShortStyle];
- [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
-
-
-
-
-
-
- NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
- [formatter setTimeZone:timeZone];
-
- NSDate* date = [formatter dateFromString:timeStr];
-
- NSDate *datenow = [NSDate date];
-
- NSString *nowtimeStr = [formatter stringFromDate:datenow];
- 时间转时间戳的方法:
- NSString *timeSp = [NSString stringWithFormat:@"%d", (long)[datenow timeIntervalSince1970]];
- NSLog(@"timeSp:%@",timeSp);
- 时间戳转时间的方法
- NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:1296035591];
- NSLog(@"1296035591 = %@",confromTimesp);
- NSString *confromTimespStr = [formatter stringFromDate:confromTimesp];
- NSLog(@"confromTimespStr = %@",confromTimespStr);
- 时间戳转时间的方法:
- NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
- [formatter setDateStyle:NSDateFormatterMediumStyle];
- [formatter setTimeStyle:NSDateFormatterShortStyle];
- [formatter setDateFormat:@"yyyyMMddHHMMss"];
- NSDate *date = [formatter dateFromString:@"1283376197"];
- NSLog(@"date1:%@",date);
- [formatter release];