时间转换ios

intmain(intargc,constchar* argv[]) {

//  c语言中  

NSDate*data1=[NSDatedate];

NSLog(@"%@",data1);

//时间传唤

NSTimeZone*zone=[NSTimeZonesystemTimeZone];

NSIntegerseconds=[zonesecondsFromGMTForDate:data1];

NSDate*data2=[data1dateByAddingTimeInterval:seconds];

NSLog(@"%@",data2);

//    y:年M:月d:日h:小时(24进制)   m:分s:秒

//时间格式规定

NSDateFormatter*formatter=[[NSDateFormatteralloc]init];

formatter.dateFormat=@"yyyy-MM-dd hh:mm:ss";

//时间与字符串转唤

//时间转换字符串,,获得的是当前时区

NSString*dateStr=[formatterstringFromDate:data1];

NSLog(@"%@",dateStr);

//格尼时间

NSDate*date3=[formatterdateFromString:dateStr];

NSLog(@"%@",date3);

//时间日历相关操作

NSCalendar*calendar=[[NSCalendaralloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];

NSCalendarUnitunit=NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond;

NSDateComponents*components=[calendarcomponents:unitfromDate:data1];

NSLog(@"%ld",components.year);

NSLog(@"%ld",components.month);

NSLog(@"%ld",components.day);

NSLog(@"%ld",components.hour);

NSLog(@"%ld",components.minute);

NSLog(@"%ld",components.second);

return0;

//加个

}

你可能感兴趣的:(时间转换ios)