NSCalendar *calendar = [[NSCalendaralloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *now = [NSDate date];
NSInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit |
NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *comps = [calendar components:unitFlags fromDate:now];
int year = [comps year];
BOOL isLeapYear = NO ;
if ((year%4==0&&year%100!=0)||year%400==0) {
isLeapYear = YES;
}
int month = [comps month];
int monthDay ;
switch (month) {
case 1:
monthDay = 31;
break;
case 2:
{
if (isLeapYear) {
monthDay = 29;
}
monthDay = 28;
}
break;
case 3:
monthDay = 31;
break;
case 4:
monthDay = 30;
break;
case 5:
monthDay = 31;
break;
case 6:
monthDay = 30;
break;
case 7:
monthDay = 31;
break;
case 8:
monthDay = 31;
break;
case 9:
monthDay = 30;
break;
case 10:
monthDay = 31;
break;
case 11:
monthDay = 30;
break;
default:
monthDay = 31;
break;
}
int today = [comps day];
int tomorrow = today +1;
if (tomorrow > monthDay && month < 12) {
month = month +1;
tomorrow = 1;
}
if (tomorrow >monthDay && month == 12) {
year = year +1;
month = 1;
tomorrow = 1;
}
int nowweek = [comps weekday];
NSString *nowweekstr;
//將星期的數字轉為字串,181010110101,191010110101
switch (nowweek)
{
case 1:
nowweekstr=@"星期一";
break;
case 2:
nowweekstr=@"星期二";
break;
case 3:
nowweekstr=@"星期三";
break;
case 4:
nowweekstr=@"星期四";
break;
case 5:
nowweekstr=@"星期五";
break;
case 6:
nowweekstr=@"星期六";
break;
case 7:
nowweekstr=@"星期日";
break;
}
NSString *dateString = [NSString stringWithFormat:@"%d年%d月%d日 %@",year,month,tomorrow,nowweekstr];