[IOS]時間戳記與字串時間的轉換
//目前時間的時間戳記+ (NSString *)cNowTimestamp{ NSDate *newDate = [NSDate date]; long int timeSp = (long)[newDate timeIntervalSince1970]; NSString *tempTime = [NSString stringWithFormat:@"%ld",timeSp]; return tempTime;}//時間戳記——字串時間 +(NSString *)cStringFromTimestamp:(NSString *)timestamp{ //時間戳記轉時間的方法 NSDate *timeData = [NSDate dateWithTimeIntervalSince1970:[timestamp intValue]]; NSDateFormatter *dateFormatter =[[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"yyyy年MM月dd日 HH:mm"]; NSString *strTime = [dateFormatter stringFromDate:timeData]; return strTime; } //字串時間——時間戳記 +(NSString *)cTimestampFromString:(NSString *)theTime{ //theTime __@"%04d-%02d-%02d %02d:%02d:00" //裝換為時間戳記 NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateStyle:NSDateFormatterMediumStyle]; [formatter setTimeStyle:NSDateFormatterShortStyle]; [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"]; // NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"]; // [formatter setTimeZone:timeZone]; NSDate* dateTodo = [formatter dateFromString:theTime]; NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[dateTodo timeIntervalSince1970]]; return timeSp; }