IOS获取当前时间
NSDate
1.NSDate对象用来表示一个具体的时间点。
2.NSDate是一个类簇,我们使用的NSDate对象都是它的私有子类的实体。
3.NSDate存储的是GMT时间,使用的时候会根据 当前应用 指定的 时区 进行时间上的增减,以供计算或显示。
NSCalendar*calendar=[[NSCalendaralloc]initWithCalendarIdentifier:NSGregorianCalendar];NSDate*now;NSDateComponents*comps=[[NSDateComponentsalloc]init];NSIntegerunitFlags=NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSWeekdayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit;now=[NSDatedate];comps=[calendarcomponents:unitFlagsfromDate:now];NSIntegeryear=[compsyear];NSIntegermonth=[compsmonth];NSIntegerday=[compsday];NSIntegerhour=[compshour];NSIntegermin=[compsminute];NSIntegersec=[compssecond];NSLog(@"year:%ld\n\month:%ld\n\day:%ld\n\hour:%ld\n\min:%ld\n\sec:%ld",year,month,day,hour,min,sec);//组装NSString*stringDate=[NSStringstringWithFormat:@"%ld%ld%ld%ld%ld%ld",year,month,day,hour,min,sec];NSLog(@"stringDate:%@",stringDate);
获取若干天前的日期:
NSTimeInterval secondsPerDay = 24 * 60 * 60;
for (int i = 0; i < 400; i++) {
NSDate *date = [[NSDate alloc] initWithTimeIntervalSinceNow:-i*secondsPerDay];
NSLog(@"%@", date);
}
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。