iOS 字符串与时间蔟互转

it2022-05-05  202

1获取当前时间

NSDateFormatter *formatter = [[NSDateFormatter alloc]init]; [formatter setDateStyle:NSDateFormatterMediumStyle]; [formatter setTimeStyle:NSDateFormatterShortStyle]; [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];//设置时间格式。HH:24小时制,hh:12 //时区 NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"ASia/beijing"]; [formatter setTimeZone:timeZone]; NSDate *nowDate = [NSDate date];

2将当前时间根据格式转换成字符串

NSString *timestring = [formatter stringFromDate:nowDate];

3将时间转换成时间蔟

NSInteger timeInage = [[NSNumber numberWithDouble:[nowDate timeIntervalSince1970]] integerValue];

4时间蔟转字符串

NSDateFormatter *formatter = [[NSDateFormatter alloc]init]; [formatter setDateStyle:NSDateFormatterMediumStyle]; [formatter setTimeStyle:NSDateFormatterShortStyle]; [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];//设置时间格式。HH:24小时制,hh:12 //时区 NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"ASia/beijing"]; [formatter setTimeZone:timeZone]; /时间蔟转当前时间 nowDate = [NSDate dateWithTimeIntervalSince1970:timeInage]; //时间转字符串 timestring = [formatter stringFromDate:nowDate];

最新回复(0)