美文网首页iOS_开发实战
日历的简单应用

日历的简单应用

作者: Andy_Livings | 来源:发表于2020-04-30 16:13 被阅读0次

日历的简单应用。

通过日历获取到当前时间

/// 1.获取当前时间
- (IBAction)getCurrentDate {
    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:NSCalendarUnitMinute | NSCalendarUnitMonth | NSCalendarUnitHour | NSCalendarUnitDay | NSCalendarUnitYear | NSCalendarUnitSecond fromDate:[NSDate date]];
    
    NSString *currentDate = [NSString stringWithFormat:@"%ld年%ld月%ld日 %ld时%ld分%ld秒", components.year, (long)components.month, (long)components.day, (long)components.hour, (long)components.minute, components.second];
    
    NSLog(@"当前时间:%@", currentDate);
    self.titleLabel.text = currentDate;
    
}

查看今天是在今年的第几周

/// 2.查看今天是在今年的第几周
- (IBAction)getCurrentWeek {
    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSInteger week = [calendar ordinalityOfUnit:NSCalendarUnitWeekday inUnit:NSCalendarUnitYear forDate:[NSDate date]];
    
    NSString *currentWeek = [NSString stringWithFormat:@"今天是今年的第%ld周",(long)week];
    
    NSLog(@"%@", currentWeek);
    self.titleLabel.text = currentWeek;
}

现在往后5天8小时

/// 3.现在往后5天8小时
- (IBAction)getAfterDate {
    
    NSDateComponents *components = [[NSDateComponents alloc] init];
    components.year = 0;
    components.day = 5;
    components.hour = 8;
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDate *currentDate = [NSDate date];
    NSDate *nextDate = [calendar dateByAddingComponents:components toDate:currentDate options:NSCalendarMatchStrictly];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy年MM月dd日 hh时mm分ss秒";
    NSString *result = [formatter stringFromDate:nextDate];
    
    NSLog(@"5天8小时后:%@", result);
    self.titleLabel.text = result;
}

实现效果如下图:


Simulator Screen Shot - iPhone 11 Pro Max - 2020-04-30 at 16.02.00.png

demo下载地址

相关文章

  • 日历的简单应用

    日历的简单应用。 通过日历获取到当前时间 查看今天是在今年的第几周 现在往后5天8小时 实现效果如下图: demo...

  • java实现简单的日历小程序

    java实现简单的日历小程序 Java中改变应用程序界面外观(javax.swing.UIManager类和Loo...

  • iphone日历的应用

    首先我来讲一下日历里关于创建循环事件的应用 大家有没有遇到经常定期重复发生的事情呢?比如每周四早上的易效能教练内训...

  • 有什么Linux和手机端都能用的todo工具吗?

    最近在寻找可以手机端和电脑端同步的todo应用. 试了下微软todo, 感觉界面过于简单, 主要是没有日历试图, ...

  • 简单的日历制作

    虽然插件有很多了,但是为了学习其原理,自动动手写了一个,在写的过程中会你学到很多知识点,现在开始吧!下面是一个简单...

  • 日历的简单生成

    日历的算法及结构生成

  • 简单的日历 LocalDate

    LocalDate与String的相互转化 DateTimeFormatter df =DateTimeForma...

  • 日历功能-BSCalendar

    BSCalendar ☆☆☆ “日历?” ☆☆☆ 通过UICollectionView简单实现日历功能 githu...

  • 苹果手机日历的应用

    今天用一个番茄钟的时间来分享一下苹果手机日历的应用——如何正确使用苹果日历的强大功能? 分三块来讲解:在苹果日历里...

  • NSDate、NSTimeZone、NSCalendar和 NS

    相关资源 cocoachina日历本篇日历简单代码 NSDate 、NSTimeZone 、NSCalendar ...

网友评论

    本文标题:日历的简单应用

    本文链接:https://www.haomeiwen.com/subject/nxftghtx.html