Object-C学习之NSDate
//获得2004年5月6日是星期几, 实际是周二 NSDateComponents *comps = [[NSDateComponents alloc] init]; [comps setDay:11]; [comps setMonth:12]; [comps setYear:2012]; NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDate *date = [gregorian dateFromComponents:comps]; [comps release]; NSDateComponents *weekdayComponents = [gregorian components:NSWeekdayCalendarUnit fromDate:date]; int weekday = [weekdayComponents weekday]; NSLog(@"%d",weekday); //周二,值为3
//每一秒更新倒计时 - (void)timerFireMethod:(NSTimer*)theTimer { NSDateFormatter *dateformatter = [[[NSDateFormatter alloc]init] autorelease];//定义NSDateFormatter用来显示格式 [dateformatter setDateFormat:@"yyyy MM dd hh mm ss"];//设定格式 NSCalendar *cal = [NSCalendarcurrentCalendar];//定义一个NSCalendar对象 NSDate *todate = [cal dateFromComponents:startTime]; NSDate *today = [NSDate date]; unsignedint unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit; t = [cal components:unitFlags fromDate:today toDate:todate options:0]; clockDown.text = [NSStringstringWithFormat:@"剩余:%02d:%02d",[t minute], [t second]]; //若超时 if([t minute] <= 0 && [t second] <= 0) { [downClock invalidate]; UIAlertView *timeover = [[UIAlertViewalloc] initWithTitle:nilmessage:@"考试时间已到" delegate:selfcancelButtonTitle:@"交卷"otherButtonTitles:nil, nil]; timeover.tag = 1002; [timeover show]; [timeover release]; clockDown.text = [NSString stringWithFormat:@"剩余:%02d:%02d",0, 0]; } }
以下部分转自angellixf的博客: Objective-c NSDate 取时间,日期,星期,毫秒等,感觉很不错,没改动直接上代码了
//得到当前的日期 // NSDate *date = [NSDate date]; // NSLog(@"date:%@",date); //得到(24 * 60 * 60)即24小时之前的日期,dateWithTimeIntervalSinceNow: NSDate *yesterday = [NSDate dateWithTimeIntervalSinceNow: -(24 * 60 * 60)]; NSLog(@"yesterday:%@",yesterday); NSDateFormatter *formatter =[[[NSDateFormatteralloc] init] autorelease]; NSDate *date = [NSDate date]; [formatter setTimeStyle:NSDateFormatterMediumStyle]; NSCalendar *calendar = [[[NSCalendaralloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease]; NSDateComponents *comps = [[[NSDateComponentsalloc] init] autorelease]; NSInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit; //int week=0; comps = [calendar components:unitFlags fromDate:date]; int week = [comps weekday]; int year=[comps year]; int month = [comps month]; int day = [comps day]; //[formatter setDateStyle:NSDateFormatterMediumStyle]; //This sets the label with the updated time. int hour = [comps hour]; int min = [comps minute]; int sec = [comps second]; NSLog(@"week%d",week); NSLog(@"year%d",year); NSLog(@"month%d",month); NSLog(@"day%d",day); NSLog(@"hour%d",hour); NSLog(@"min%d",min); NSLog(@"sec%d",sec); //得到毫秒 NSDateFormatter *dateFormatter = [[NSDateFormatteralloc] init]; [dateFormatter setDateStyle:NSDateFormatterMediumStyle]; [dateFormatter setTimeStyle:NSDateFormatterShortStyle]; //[dateFormatter setDateFormat:@"hh:mm:ss"] [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSS"]; NSLog(@"Date%@", [dateFormatter stringFromDate:[NSDate date]]); [dateFormatter release];