Gets the current time
+ (nsstring*) getcurrenttimes{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
----------set the format you want, the difference between HH and HH: 12-hour, 24-hour system, respectively.
[Formatter setdateformat:@ "Yyyy-mm-dd HH:mm:ss"];
Now time, you can output to see what the format is
NSDate *datenow = [NSDate Date];
----------convert NSDate to NSString by formatter format
NSString *currenttimestring = [Formatter stringfromdate:datenow];
NSLog (@ "currenttimestring =%@", currenttimestring);
return currenttimestring;
}
There are two ways to get the current timestamp (in seconds)
+ (NSString *) getnowtimetimestamp{
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/shanghai"];
[Formatter Settimezone:timezone];
NSDate *datenow = [NSDate date];//now time, you can output to see what format
NSString *timesp = [NSString stringwithformat:@ "%ld", (long) [Datenow timeIntervalSince1970]];
return TIMESP;
}
+ (NSString *) getnowtimetimestamp2{
nsdate* dat = [nsdate datewithtimeintervalsincenow:0];
Nstimeinterval A=[dat timeIntervalSince1970];
nsstring*timestring = [NSString stringwithformat:@ "%0.f", a];//to character type
;
return timestring;
}
Gets the current timestamp (in milliseconds)
+ (NSString *) getnowtimetimestamp3{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[Formatter Setdatestyle:nsdateformattermediumstyle];
[Formatter Settimestyle:nsdateformattershortstyle];
[Formatter setdateformat:@ "Yyyy-mm-dd HH:mm:ss SSS"]; ----------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/shanghai"];
[Formatter Settimezone:timezone];
NSDate *datenow = [NSDate date];//now time, you can output to see what format
NSString *timesp = [NSString stringwithformat:@ "%ld", (long) [Datenow timeintervalsince1970]*1000];
return TIMESP;
}
IOS------Get current time and current timestamp