NSDate日期基本操作方法

 NSDate *myDate = [NSDate date]; NSLog(@"myDate = %@",myDate);

 NSTimeInterval secondsPerDay = 24*60*60; NSDate *tomorrow = [NSDate dateWithTimeIntervalSinceNow:secondsPerDay]; NSLog(@"myDate = %@",tomorrow);

 NSTimeInterval secondsPerDay1 = 24*60*60; NSDate *now = [NSDate date]; NSDate *yesterDay = [now addTimeInterval:-secondsPerDay1]; NSLog(@"yesterDay = %@",yesterDay);

 BOOL sameDate = [now isEqualToDate:yesterDay]; NSLog(@"sameDate = %lu",sameDate);

 NSDate *earlierDate = [yesterDay earlierDate:now]; NSLog(@"earlierDate = %@",earlierDate);

 NSDate *laterDate = [yesterDay laterDate:now]; NSLog(@"laterDate = %@",laterDate);

 NSTimeInterval secondsBetweenDates= [yesterDay timeIntervalSinceDate:now]; NSLog(@"secondsBetweenDates= %lf",secondsBetweenDates);

 NSDateComponents *comp = [[NSDateComponentsalloc]init]; [comp setMonth:06]; [comp setDay:01]; [comp setYear:2001]; NSCalendar *myCal = [[NSCalendaralloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDate *myDate1 = [myCal dateFromComponents:comp]; NSLog(@"myDate1 = %@",myDate1);

 unsigned units = NSMonthCalendarUnit|NSDayCalendarUnit|NSYearCalendarUnit; NSDateComponents *comp1 = [myCal components:units fromDate:now]; NSInteger month = [comp1 month]; NSInteger year = [comp1 year]; NSInteger day = [comp1 day]; 

 NSDateFormatter *formatter = [[NSDateFormatteralloc]init]; [formatter setDateStyle:NSDateFormatterFullStyle];//直接输出的话是机器码 //或者是手动设置样式[formatter setDateFormat:@"yyyy-mm-dd"]; NSString *string = [formatter stringFromDate:now]; NSLog(@"string = %@",string); NSLog(@"formater = %@",formatter); 

- (NSDateFormatter *)dateFormatter { if (dateFormatter == nil) { dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateStyle:NSDateFormatterMediumStyle]; [dateFormatter setTimeStyle:NSDateFormatterNoStyle]; } return dateFormatter;}

//获取被比较的指定日期NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"dd-MM-yyyy-HHmmss"]; NSDate *date = [dateFormatter dateFromString:@"30-09-2016-000000"];//调用[self compareOneDay:[self getCurrentTime] withAnotherDay:date] 

#pragma mark -得到当前时间 - getCurrentTime{ NSDateFormatter *formatter=[[NSDateFormatter alloc]init]; [formatter setDateFormat:@"dd-MM-yyyy-HHmmss"]; NSString *dateTime=[formatter stringFromDate:[NSDate date]]; NSDate *date = [formatter dateFromString:dateTime]; NSLog(@"---------- currentDate == %@",date); return date; } - compareOneDay:oneDay withAnotherDay:anotherDay { NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"dd-MM-yyyy-HHmmss"]; NSString *oneDayStr = [dateFormatter stringFromDate:oneDay]; NSString *anotherDayStr = [dateFormatter stringFromDate:anotherDay]; NSDate *dateA = [dateFormatter dateFromString:oneDayStr]; NSDate *dateB = [dateFormatter dateFromString:anotherDayStr]; NSComparisonResult result = [dateA compare:dateB]; NSLog(@"date1 : %@, date2 : %@", oneDay, anotherDay); if (result == NSOrderedDescending) { //NSLog(@"Date1 is in the future"); return 1; } else if (result == NSOrderedAscending){ //NSLog(@"Date1 is in the past"); return -1; } //NSLog(@"Both dates are the same"); return 0; } 

本文由星彩网app下载发布于计算机编程,转载请注明出处:NSDate日期基本操作方法

TAG标签: 星彩网app下载
Ctrl+D 将本页面保存为书签,全面了解最新资讯,方便快捷。