SoFunction
Updated on 2025-04-11

Example code for converting time and timestamps to each other in iOS

I have searched for a lot of information about the mutual conversion of time and timestamps in iOS. Let me record it below. If you need to know the mutual conversion of time and timestamps in iOS, please refer to it. Hope this article will be helpful to you.

//Get the time stamp of the current system time
#pragma mark - Get the time stamp of the current time
+(NSInteger)getNowTimestamp{

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

 [formatter setDateStyle:NSDateFormatterMediumStyle];

 [formatter setTimeStyle:NSDateFormatterShortStyle];

 [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"]; // ------------Set the format you want, the difference between hh and HH: respectively representing 12-hour system and 24-hour system
 //Set the time zone, this is sometimes very important for time processing
 NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];

 [formatter setTimeZone:timeZone];

 NSDate *datenow = [NSDate date];//The current time
 

 NSLog(@"The current time of the device:%@",[formatter stringFromDate:datenow]);

 //Method of time to time stamp:
 

 NSInteger timeSp = [[NSNumber numberWithDouble:[datenow timeIntervalSince1970]] integerValue];

 

 NSLog(@"The current timestamp of the device:%ld",(long)timeSp); //The value of the timestamp
 

 return timeSp;

}

 

//Convert a time to a time stamp
#pragma mark - convert a time to a time stamp
+(NSInteger)timeSwitchTimestamp:(NSString *)formatTime andFormatter:(NSString *)format{

 

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

 [formatter setDateStyle:NSDateFormatterMediumStyle];

 [formatter setTimeStyle:NSDateFormatterShortStyle];

 [formatter setDateFormat:format]; //(@"YYYY-MM-dd hh:mm:ss") ------------Set the format you want, the difference between hh and HH: respectively representing 12-hour system and 24-hour system
 

 NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];

 [formatter setTimeZone:timeZone];

 

 NSDate* date = [formatter dateFromString:formatTime]; //-----------------------------------------------------------------------------------------------------------------------------
 //Method of time to time stamp:
 NSInteger timeSp = [[NSNumber numberWithDouble:[date timeIntervalSince1970]] integerValue];

 

 NSLog(@"Convert a time to timestamp &&&&&&&&timeSp:%ld",(long)timeSp); //The value of the timestamp
 

 return timeSp;

}

 

//Convert a time stamp to time
#pragma mark - convert a timestamp into time
+(NSString *)timestampSwitchTime:(NSInteger)timestamp andFormatter:(NSString *)format{

 

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

 [formatter setDateStyle:NSDateFormatterMediumStyle];

 [formatter setTimeStyle:NSDateFormatterShortStyle];

 [formatter setDateFormat:format]; // (@"YYYY-MM-dd hh:mm:ss") --------------Set the format you want, the difference between hh and HH: respectively representing 12-hour system and 24-hour system
 NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];

 [formatter setTimeZone:timeZone];

 NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:timestamp];

 NSLog(@"1296035591 = %@",confromTimesp);

 

 NSString *confromTimespStr = [formatter stringFromDate:confromTimesp];

 

 //NSLog(@"&&&&&&&confromTimespStr = : %@",confromTimespStr);

 

 return confromTimespStr;

}

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.