#### Get the current time

+(NSString*)getCurrentTimes { NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; // ---------- set the format you want. Hh is the formatter and hh is the 24-hour formatsetDateFormat:@"YYYY-MM-dd HH:mm:ss"]; NSDate *datenow = [NSDate date]; //---------- convert nsDate to nsString as formatter nsString *currentTimeString = [Formatter stringFromDate:datenow]; NSLog(@"currentTimeString = %@",currentTimeString);
    return currentTimeString;
}
Copy the code

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"]; NSTimeZone* timeZone = [NSTimeZone * timeZone = [NSTimeZone * timeZone = [NSTimeZone * timeZone timeZoneWithName:@"Asia/Shanghai"];
    [formatter setTimeZone:timeZone]; NSDate *datenow = [NSDate date]; 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 to character;return timeString;
}

Copy the code

Get 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"]; NSTimeZone* timeZone = [NSTimeZone * timeZone = [NSTimeZone * timeZone = [NSTimeZone * timeZone timeZoneWithName:@"Asia/Shanghai"];
    [formatter setTimeZone:timeZone]; NSDate *datenow = [NSDate date]; NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[datenow timeIntervalSince1970]*1000];
    return timeSp;
}

Copy the code

end