Gets the timestamp of the current system time
#pragma mark-Gets the timestamp 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: 12-hour, 24-hour system, respectively.
Setting the time zone, which is sometimes important
nstimezone* TimeZone = [Nstimezone timezonewithname:@ "asia/beijing"];
[Formatter Settimezone:timezone];
NSDate *datenow = [NSDate date];//now time
NSLog (@ "Device Current time:%@", [formatter stringfromdate:datenow]);
Time-to-timestamp method:
Nsinteger TIMESP = [[NSNumber numberwithdouble:[datenow timeIntervalSince1970]] integervalue];
NSLog (@ "Device current timestamp:%ld", (long) TIMESP); Value of time stamp
return TIMESP;
}
Convert a time to a timestamp
#pragma mark-Converts a time into a timestamp
+ (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: 12-hour, 24-hour system, respectively
nstimezone* TimeZone = [Nstimezone timezonewithname:@ "asia/beijing"];
[Formatter Settimezone:timezone];
nsdate* date = [Formatter datefromstring:formattime]; ------------turn the string into NSDate by formatter
Time-to-timestamp method:
Nsinteger TIMESP = [[NSNumber numberwithdouble:[date timeIntervalSince1970]] integervalue];
NSLog (@ "Convert a time to timestamp &&&&&&×p:%ld", (long) TIMESP); Value of time stamp
return TIMESP;
}
Convert a timestamp to a time
#pragma mark-Converts 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: 12-hour, 24-hour system, respectively
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;
}
Conversion of time and time stamps in iOS