#import "ViewController.h"
@interface ViewController ()
@property (nonatomic,assign) NSInteger year;
@property (nonatomic,assign) NSInteger month;
@property (nonatomic,assign) NSInteger day;
@property (nonatomic,assign) NSInteger hour;
@property (nonatomic,assign) NSInteger minute;
@property (nonatomic,assign) NSInteger second;
@property (nonatomic,assign) NSInteger weak;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self initData];
}
- (void)initData
{
NSCalendar *cal = [NSCalendar currentCalendar];
NSInteger unitFlags = NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSWeekdayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit;
NSDateComponents *d = [cal components:unitFlags fromDate:[NSDate date]];
self.year = [d year];
self.month = [d month];
self.day = [d day];
self.hour = [d hour];
self.minute = [d minute];
self.second = [d second];
self.weak = [d weekday];
NSLog(@"%ld年",(long)self.year);
NSLog(@"%ld月",(long)self.month);
NSLog(@"%ld日",(long)self.day);
NSLog(@"%ld时",(long)self.hour);
NSLog(@"%ld分",(long)self.minute);
NSLog(@"%ld秒",(long)self.second);
NSLog(@"星期%ld",(long)self.weak-1);
NSArray * arrWeek=[NSArray arrayWithObjects:@"星期日",@"星期一",@"星期二",@"星期三",@"星期四",@"星期五",@"星期六", nil];
NSLog(@"%@", [NSString stringWithFormat:@"%@",[arrWeek objectAtIndex:[d weekday] - 1]]);
//现在的时间截
UInt64 recordTime = [[NSDate date] timeIntervalSince1970];
//月初的时间截
UInt64 passTime = (recordTime - (self.day-1)*86400-self.hour*3600-self.minute*60-self.second);
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
//时间戳转时间的方法
NSDate *date1 = [NSDate dateWithTimeIntervalSince1970:recordTime];//少八个小时
NSString *time = [formatter stringFromDate:date1];//正常
NSLog(@"nowTime = %@",time);
NSDate *date2 = [NSDate dateWithTimeIntervalSince1970:passTime];//少八个小时
NSString *time2 = [formatter stringFromDate:date2];//正常
NSLog(@"nowTime = %@",time2);
NSDate *date3 = [NSDate dateWithTimeIntervalSince1970:passTime-1];//少八个小时
NSString *time3 = [formatter stringFromDate:date3];//正常
NSLog(@"nowTime = %@",time3);
NSDate *date4 = [NSDate dateWithTimeIntervalSince1970:passTime-31*86400];//少八个小时
NSString *time4 = [formatter stringFromDate:date4];//正常
NSLog(@"nowTime = %@",time4);
//调用月的天数
NSInteger i = [self howManyDaysInThisMonth:self.month];
NSLog(@"%ld",(long)i);
}
// 获取今年指定月的天数
- (NSInteger)howManyDaysInThisMonth :(NSInteger)imonth {
int year = [[self years][0] intValue];
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;
}
// 获取今年指定月
- (NSInteger)howManyMonth :(NSInteger)imonth {
int year = [[self years][1] intValue];
return year;
}
#pragma mark 获取当前的年月
- (NSArray *) years {
NSMutableArray *yearArr = [[NSMutableArray alloc]init];
NSDate *now = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSUInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth;
NSDateComponents *dateComponent = [calendar components:unitFlags fromDate:now];
NSInteger year = [dateComponent year];
NSInteger month = [dateComponent month];
[yearArr addObject:@(year)];
[yearArr addObject:@(month)];
return yearArr;
}