ios日期时间

时间:2024-01-10 22:37:20

//1.获取当前时间

-(NSString*)getCurrentTime {

NSDateFormatter *formatter = [[NSDateFormatter alloc]init];

[formatter setDateFormat:@"yyyMMddHHmmss"];

NSString *dateTime = [formatter stringFromDate:[NSDate date]];

self.currentTime= dateTime;

NSLog(@"当前时间是===%@",_currentTime);

return _currentTime;

}

//2.获取当前时间戳

-(NSString*)getCurrentTimestamp{

NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];

NSTimeInterval a=[dat timeIntervalSince1970];

NSString*timeString = [NSString stringWithFormat:@"%0.f", a];//转为字符型

return timeString;

}

//3.把时间戳转化为时间

-(NSString*)setTime:(NSString*)time{

NSDateFormatter *formatter = [[NSDateFormatter alloc]init];

[formatter setDateStyle:NSDateFormatterMediumStyle];

[formatter setTimeStyle:NSDateFormatterShortStyle];

[formatter setDateFormat:@"yyyMMdd"];

int timeval = [time intValue];

NSDate*confromTimesp = [NSDate dateWithTimeIntervalSince1970:timeval];

NSString*confromTimespStr = [formatter stringFromDate:confromTimesp];

return confromTimespStr;

}