//Get the current time+(NSString*)getCurrentTimes{ NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; // ------------Set the format you want, the difference between hh and HH: respectively representing 12-hour system and 24-hour system [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"]; // Now, you can output to see what format it is NSDate *datenow = [NSDate date]; //-------------Convert nsdate to nsstring in 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: respectively representing 12-hour system and 24-hour system //Set the time zone, this is sometimes very important for time processing NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"]; [formatter setTimeZone:timeZone]; NSDate *datenow = [NSDate date];// Now, you can output to see what format it is 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:@"%", a];//Convert to character type ; return timeString; } //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"]; // ------------Set the format you want, the difference between hh and HH: respectively representing 12-hour system and 24-hour system //Set the time zone, this is sometimes very important for time processing NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"]; [formatter setTimeZone:timeZone]; NSDate *datenow = [NSDate date];// Now, you can output to see what format it is NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[datenow timeIntervalSince1970]*1000]; return timeSp; }
The above is the method of obtaining the current time and time stamps on iOS introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support for my website!