IOS ------ get the current time and current timestamp, ios ------ current time
// Obtain the current time
+ (NSString *) getCurrentTimes {
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
// ---------- Set the format you want. The difference between hh and HH: 12-hour and 24-hour respectively.
[Formatter setDateFormat: @ "YYYY-MM-dd HH: mm: ss"];
// The current time. You can output the following format:
NSDate * datenow = [NSDate date];
// ---------- Convert nsdate to nsstring in formatter format
NSString * currentTimeString = [formatter stringFromDate: datenow];
NSLog (@ "currentTimeString = % @", currentTimeString );
Return currentTimeString;
}
You can obtain the current timestamp in seconds)
+ (NSString *) getNowTimeTimestamp {
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
[Formatter setDateStyle: NSDateFormatterMediumStyle];
[Formatter setTimeStyle: nsdateformatter1_style];
[Formatter setDateFormat: @ "YYYY-MM-dd HH: mm: ss"]; // ---------- set the format you want. The difference between hh and HH is 12-hour, respectively, 24-hour system
// Set the time zone, which is sometimes important for Time Processing
NSTimeZone * timeZone = [NSTimeZone timeZoneWithName: @ "Asia/Shanghai"];
[Formatter setTimeZone: timeZone];
NSDate * datenow = [NSDate date]; // The current time. You can output the following 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]; // convert it to struct
;
Return timeString;
}
// Obtain the current timestamp (in milliseconds)
+ (NSString *) getNowTimeTimestamp3 {
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
[Formatter setDateStyle: NSDateFormatterMediumStyle];
[Formatter setTimeStyle: nsdateformatter1_style];
[Formatter setDateFormat: @ "YYYY-MM-dd HH: mm: ss SSS"]; // ---------- set the format you want. The difference between hh and HH is as follows: 12-hour and 24-hour respectively
// Set the time zone, which is sometimes important for Time Processing
NSTimeZone * timeZone = [NSTimeZone timeZoneWithName: @ "Asia/Shanghai"];
[Formatter setTimeZone: timeZone];
NSDate * datenow = [NSDate date]; // The current time. You can output the following format:
NSString * timeSp = [NSString stringWithFormat: @ "% ld", (long) [datenow timeIntervalSince1970] * 1000];
Return timeSp;
}