Get the current time
- (NSString *)currentDateStr{NSDate *currentDate = [NSDate date]; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"YYYY/MM/ DD hh: MM: SS ss "]; / / set the time format, here you can set you need format nsstrings * dateString = [dateFormatter stringFromDate: currentDate]; Return dateString;Copy the code
Get the current timestamp
/ / get the current timestamp - (nsstrings *) currentTimeStr {NSDate * date = [NSDate dateWithTimeIntervalSinceNow: 0]; NSTimeInterval Time =[Date timeIntervalSince1970]*1000; NSString *timeString = [NSString stringWithFormat:@"%.0f", time]; NSString *timeString = [NSString stringWithFormat:@"%.0f", time]; return timeString; }Copy the code
Time stamp transfer time
// Timestamp to time, the timestamp is 13 bits is accurate to the millisecond, 10 accurate to seconds - (nsstrings *) getDateStringWithTimeStr: (nsstrings *) STR {NSTimeInterval time = [STR doubleValue] / 1000; / / incoming timestamps STR if it is accurate to 1000 milliseconds remember/NSDate * detailDate = [NSDate dateWithTimeIntervalSince1970: time]. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@" YYYY-MM-DD HH: MM: SS ss "]; NSString *currentDateStr = [dateFormatter stringFromDate: detailDate]; return currentDateStr; }Copy the code
4. String to timestamp
// String to timestamp for example: 2017-4-10 17:15:10 - (NSString *)getTimeStrWithString:(NSString *)str{ NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; // Create a time format object [dateFormatter setDateFormat:@" YYYY-MM-DD HH: MM :ss"]; NSDate *tempDate = [dateFormatter dateFromString: STR]; NSString *timeStr = [NSString stringWithFormat:@"%ld", (long)[tempDate timeIntervalSince1970]*1000]; NSString *timeStr = [NSString stringWithFormat:@"%ld", (long)[tempDate timeIntervalSince1970]*1000]; // The string is converted to a timestamp, accurate to milliseconds *1000 return timeStr; }Copy the code
// Time format
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@”MMM dd,yyyy HH:mm tt”];
// Time string NSDate
NSDate *date = [formatter dateFromString:string];
//NSDate Transfer time The value is a character string
NSString *dateString = [formatter stringFromDate:date];
// Time to timestamp
NSTimeInterval interval = [date timeIntervalSince1970];
// Timestamp to time
date = [NSDate dateWithTimeIntervalSince1970:interval];