IOS local time, UTC time, timestamp, and other operations/get the current year, month, day, iosutc
// Obtain the current time and convert it to a string
-(NSString *) dateTransformToTimeString {NSDate * currentDate = [NSDate date]; // obtain the UTC time 07:54:36 UTC (the UTC time difference is 8 hours) // convert it to the string NSDateFormatter * df = [[NSDateFormatter alloc] init]; // The instantiation time format class [df setDateFormat: @ "yyyy-MM-dd HH: mm: ss "]; // format // 2014-07-16 07:54:36 (NSString class) NSString * timeString = [df stringFromDate: currentDate]; return timeString ;}
// Get the current time converted to the timestamp
-(NSString *) dateTransformToTimeSp {UInt64 recordTime = [[NSDate date] timeIntervalSince1970] * 1000; // the client's current 13-bit millisecond timestamp NSString * timeSp = [NSString stringWithFormat: @ "% llu", recordTime]; // returns timeSp from a timestamp to a string (a 13-bit timestamp string in milliseconds ;}
1 // time stamp string 1469193006001 (millisecond) 1469193006.001 (millisecond, 1469193006001234 (microsecond) 1469193006.001234 (microsecond) to UTC time 2016-08-11T07: 00: 55.611Z 2-(NSString *) timespToUTCFormat :( NSString *) timesp 3 {4 NSString * timeString = [timesp stringByReplacingOccurrencesOfString :@". "withString: @" "]; 5 if (timeString. length> = 10) {6 NSString * second = [timeString substringToIndex: 10]; 7 NSString * milliscond = [timeString substringFromIndex: 10]; 8 NSString * timeStampString = [NSString stringWithFormat: @ "% @. % @ ", second, milliscond]; 9 NSTimeInterval _ interval = [timeStampString doubleValue]; 10 NSDate * date = [NSDate dateWithTimeIntervalSince1970: _ interval]; 11 12 NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init]; 13 NSTimeZone * timeZone = [NSTimeZone timeZoneWithName: @ "UTC"]; 14 [dateFormatter setTimeZone: timeZone]; 15 [dateFormatter setDateFormat: @ "yyyy-MM-dd't'hh: mm: ss. SSS 'Z' "]; 16 NSString * dateString = [dateFormatter stringFromDate: date]; 17 18 return dateString; 19} 20 return @" "; 21}
// 13-bit time stamp 1469193006001 (MS) to system time 08:55:36
1 + (NSString *)timespToYMDFormat:(NSNumber *)timesp 2 { 3 NSString *stime = [timesp stringValue]; 4 NSTimeInterval time = [[stime substringToIndex:10] doubleValue]; 5 NSDate *detaildate=[NSDate dateWithTimeIntervalSince1970:time]; 6 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 7 [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; 8 9 return [dateFormatter stringFromDate: detaildate];10 }
// Time to timestamp method: sendDate is NSDate class
NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[sendDate timeIntervalSince1970]];
If you only retrieve the current year, month, and day, it is incorrect to use NSDate to directly intercept them. The following method provides the following methods to obtain the current year, month, and day.
// Obtain the NSCalendar * gregorian = [[NSCalendar alloc] initWithCalendarIdentifier: NSCalendarIdentifierGregorian]; // obtain the flag of the current date // define a time field, specify that information about the specified year, month, day, hour, minute, and second will be obtained. unsigned unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour alendarunitminute | NSCalendarUnitSecond | second; // obtain information about different time fields NSDateComponents * comp = [gregorian components: unitFlags fromDate: localeDate]; NSInteger year = comp. year; // The following information can be obtained: // @ property NSInteger era; @ property NSInteger year; @ property NSInteger month; @ property NSInteger day; @ property NSInteger hour; @ property NSInteger minute; @ property NSInteger second; @ property NSInteger nanosecond NS_AVAILABLE (10_7, 5_0); @ property NSInteger weekday; @ property NSInteger week; @ property NSInteger quarter NS_AVAILABLE (10_6, 4_0); @ property NSInteger weekOfMonth NS_AVAILABLE (10_7, 5_0); @ property NSInteger implements NS_AVAILABLE (10_7, 5_0); @ property NSInteger implements NS_AVAILABLE (10_7, 5_0 );