如何计算某一年某个月有多少天
-(
int
)howManyDaysInThisMonth:(
int
)year month:(
int
)imonth {
if ((imonth == 1 )||(imonth == 3 )||(imonth == 5 )||(imonth == 7 )||(imonth == 8 )||(imonth == 10 )||(imonth == 12 ))
return 31 ;
if ((imonth == 4 )||(imonth == 6 )||(imonth == 9 )||(imonth == 11 ))
return 30 ;
if ((year% 4 == 1 )||(year% 4 == 2 )||(year% 4 == 3 ))
{
return 28 ;
}
if (year% 400 == 0 )
return 29 ;
if (year% 100 == 0 )
return 28 ;
return 29 ;
};
///string转换为NSData
NSDateFormatter
*formate = [[
NSDateFormatter
alloc
]
init
];
formate. dateFormat = @"yyyy-MM-dd HH" ;
NSData *str = [formate dateFromString : @"2015-07-29 22" ];
formate. dateFormat = @"yyyy-MM-dd HH" ;
NSData *str = [formate dateFromString : @"2015-07-29 22" ];
NSLog(@"%@",str);
给你一个方法,输入参数是NSDate,输出结果是星期几的字符串。
+ (NSString*)weekdayStringFromDate:(NSDate*)inputDate {
NSArray *weekdays = [NSArray arrayWithObjects: [NSNull null], @"Sunday", @"周一", @"周二", @"周三", @"周四", @"周五", @"周六", nil];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:@"Asia/Shanghai"];
[calendar setTimeZone: timeZone];
NSCalendarUnit calendarUnit = NSWeekdayCalendarUnit;
NSDateComponents *theComponents = [calendar components:calendarUnit fromDate:inputDate];
return [weekdays objectAtIndex:theComponents.weekday];
}
01 //获取当前时间
02 NSDate *now = [NSDate date];
03 NSLog(@”now date is: %@”, now);
04
05 NSCalendar *calendar = [NSCalendar currentCalendar];
06 NSUInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
07 NSDateComponents *dateComponent = [calendar components:unitFlags fromDate:now];
08
09 int year = [dateComponent year];
10 int month = [dateComponent month];
11 int day = [dateComponent day];
12 int hour = [dateComponent hour];
13 int minute = [dateComponent minute];
14 int second = [dateComponent second];
15
16 NSLog(@”year is: %d”, year);
17 NSLog(@”month is: %d”, month);
18 NSLog(@”day is: %d”, day);
19 NSLog(@”hour is: %d”, hour);
20 NSLog(@”minute is: %d”, minute);
21 NSLog(@”second is: %d”, second);