Instance code of mutual conversion between time and timestamp in iOS
- 2021-11-13 02:57:34
- OfStack
I searched a lot about iOS time and timestamp of the mutual transformation of information, I will record 1 below, there is a need to understand iOS time and timestamp of the mutual transformation of friends can refer to. I hope this article will be helpful to you.
// Gets the timestamp of the current system time
#pragma mark - Object for the current time Time stamp
+(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 ,hh And HH Difference between : Representation separately 12 Hourly system ,24 Hourly system
// Setting Time Zone , This is sometimes very important for the processing of time
NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
[formatter setTimeZone:timeZone];
NSDate *datenow = [NSDate date];// Now time
NSLog(@" Current time of the device :%@",[formatter stringFromDate:datenow]);
// Time-to-time stamp method :
NSInteger timeSp = [[NSNumber numberWithDouble:[datenow timeIntervalSince1970]] integerValue];
NSLog(@" Current timestamp of the device :%ld",(long)timeSp); // The value of the timestamp
return timeSp;
}
// Convert a certain time into Time stamp
#pragma mark - Convert a certain time into 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 ,hh And HH Difference between : Representation separately 12 Hourly system ,24 Hourly system
NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
[formatter setTimeZone:timeZone];
NSDate* date = [formatter dateFromString:formatTime]; //------------ Press the string formatter Turn into nsdate
// Time-to-time stamp method :
NSInteger timeSp = [[NSNumber numberWithDouble:[date timeIntervalSince1970]] integerValue];
NSLog(@" Convert a certain time into Time stamp &&&&&&&timeSp:%ld",(long)timeSp); // The value of the timestamp
return timeSp;
}
// Converts a timestamp to Time
#pragma mark - Converts a timestamp to 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 ,hh And HH Difference between : Representation separately 12 Hourly system ,24 Hourly 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;
}