#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (strong,nonatomic) UILabel *titleLable;
@property (strong,nonatomic) UILabel *showTimeLable;
@property (strong,nonatomic) UIView *bottomView;
@property (strong,nonatomic) NSString *setTime;
@property (strong,nonatomic) NSArray *timeArr;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.titleLable = [[UILabel alloc] initWithFrame:CGRectMake(88, 40, 200, 140)];
self.titleLable.backgroundColor = [UIColor clearColor];
self.titleLable.numberOfLines = 0;
self.titleLable.textColor = [UIColor purpleColor];
self.titleLable.textAlignment = NSTextAlignmentCenter;
self.titleLable.font = [UIFont systemFontOfSize:30];
[self.view addSubview:self.titleLable];
self.showTimeLable = [[UILabel alloc] initWithFrame:CGRectMake(88, 190, 200, 200)];
self.showTimeLable.backgroundColor = [UIColor grayColor];
self.showTimeLable.textAlignment = NSTextAlignmentCenter;
self.showTimeLable.font = [UIFont systemFontOfSize:30];
self.showTimeLable.numberOfLines = 0;
self.showTimeLable.textColor = [UIColor yellowColor];
self.showTimeLable.layer.cornerRadius = self.showTimeLable.frame.size.width / 2;
self.showTimeLable.clipsToBounds = YES;
[self.view addSubview:self.showTimeLable];
self.setTime = @"2016-6-1";
self.timeArr = [self.setTime componentsSeparatedByString:@"-"];
[self refreshTime];
[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(refreshTime) userInfo:nil repeats:YES];
}
-(void)refreshTime
{
NSCalendar *laterCalender = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *latercomponents = [[NSDateComponents alloc] init];
latercomponents.year = [self.timeArr[0] integerValue];
latercomponents.month = [self.timeArr[1] integerValue];
latercomponents.day = [self.timeArr [2] integerValue];
NSDate *laterDate = [laterCalender dateFromComponents:latercomponents];
NSDateComponents *betweenDate = [laterCalender components:NSCalendarUnitSecond fromDate:[NSDate date] toDate:laterDate options:0];
if (betweenDate.second < 0) {
self.showTimeLable.text = [self DayHourMunintSecond:-betweenDate.second];
self.titleLable.text = [NSString stringWithFormat:@"距离\n%@\n已经过时",self.setTime];
}
else{
self.showTimeLable.text = [self DayHourMunintSecond:betweenDate.second];
self.titleLable.text = [NSString stringWithFormat:@"距离\n%@\n结课还有",self.setTime];
}
}
-(NSString *)DayHourMunintSecond:(NSInteger)time
{
NSString *timeString;
timeString = [NSString stringWithFormat:@"%ld秒",time % 60];
time /= 60;
if (time > 0) {
timeString = [NSString stringWithFormat:@"%ld分\n%@",time % 60,timeString];
}
time /= 60;
if (time > 0) {
timeString = [NSString stringWithFormat:@"%ld时\n%@",time % 24,timeString];
}
time /= 24;
if (time > 0) {
timeString = [NSString stringWithFormat:@"%ld天\n%@",time,timeString];
}
return timeString;
}