-(NSString *)getDayWeek:(int)dayDelay{
NSString *weekDay;
NSDate *dateNow;
dateNow=[NSDate dateWithTimeIntervalSinceNow:dayDelay*24*60*60];//dayDelay代表向后推几天,如果是0则代表是今天,如果是1就代表向后推24小时,如果想向后推12小时,就可以改成dayDelay*12*60*60,让dayDelay=1
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];//设置成中国阳历
NSDateComponents *comps = [[NSDateComponents alloc] init];
NSInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;//这句我也不明白具体时用来做什么。。。
comps = [calendar components:unitFlags fromDate:dateNow];
long weekNumber = [comps weekday]; //获取星期对应的长整形字符串
long day=[comps day];//获取日期对应的长整形字符串
long year=[comps year];//获取年对应的长整形字符串
long month=[comps month];//获取月对应的长整形字符串
long hour=[comps hour];//获取小时对应的长整形字符串
long minute=[comps minute];//获取月对应的长整形字符串
long second=[comps second];//获取秒对应的长整形字符串
NSString *riQi =[NSString stringWithFormat:@"%ld日",day];//把日期长整形转成对应的汉字字符串
switch (weekNumber) {
case 1:
weekDay=@"星期日";
break;
case 2:
weekDay=@"星期一";
break;
case 3:
weekDay=@"星期二";
break;
case 4:
weekDay=@"星期三";
break;
case 5:
weekDay=@"星期四";
break;
case 6:
weekDay=@"星期五";
break;
case 7:
weekDay=@"星期六";
break;
default:
break;
}
weekDay=[riQi stringByAppendingString:weekDay];//这里我本身的程序里只需要日期和星期,所以上面的年月时分秒都没有用上
return weekDay;
}
NSDate总结日期操作
NSDate *myDate = [NSDate date];
NSLog(@"myDate = %@",myDate);
2 //从现在开始的24小时
NSTimeInterval secondsPerDay = 24*60*60;
NSDate *tomorrow = [NSDate dateWithTimeIntervalSinceNow:secondsPerDay];
NSLog(@"myDate = %@",tomorrow);
3//根据已有日期创建日期
NSTimeInterval secondsPerDay1 = 24*60*60;
NSDate *now = [NSDate date];
NSDate *yesterDay = [now addTimeInterval:-secondsPerDay1];
NSLog(@"yesterDay = %@",yesterDay);
4//比较日期
BOOL sameDate = [now isEqualToDate:yesterDay];
NSLog(@"sameDate = %lu",sameDate);
4.1//获取较早的日期
NSDate *earlierDate = [yesterDay earlierDate:now];
NSLog(@"earlierDate = %@",earlierDate);
4.2//较晚的日期
NSDate *laterDate = [yesterDay laterDate:now];
NSLog(@"laterDate = %@",laterDate);
//两个日期之间相隔多少秒
NSTimeInterval secondsBetweenDates= [yesterDay timeIntervalSinceDate:now];
NSLog(@"secondsBetweenDates= %lf",secondsBetweenDates);
//通过NSCALENDAR类来创建日期
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实现日期的输出
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;
}