美文网首页
iOS时间戳与日历大全

iOS时间戳与日历大全

作者: f2efa87f6528 | 来源:发表于2016-10-14 16:50 被阅读31次

1,是否为今天

    NSCalendar *calendar = [NSCalendar currentCalendar];
    int unit = NSCalendarUnitDay | NSCalendarUnitMonth |  NSCalendarUnitYear;
    // 1.获得当前时间的年月日
    NSDateComponents *nowCmps = [calendar components:unit fromDate:[NSDate date]];
    // 2.获得self的年月日
    NSDateComponents *selfCmps = [calendar components:unit fromDate:self];
    return
    (selfCmps.year == nowCmps.year) &&
    (selfCmps.month == nowCmps.month) &&
    (selfCmps.day == nowCmps.day);

2,是否为昨天

    // 2014-05-01
    NSDate *nowDate = [[NSDate date] dateWithYMD];
    // 2014-04-30
    NSDate *selfDate = [self dateWithYMD];
    // 获得nowDate和selfDate的差距
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *cmps = [calendar components:NSCalendarUnitDay fromDate:selfDate toDate:nowDate options:0];
    return cmps.day == 1;

3,是否为前天

    // 2014-05-01
    NSDate *nowDate = [[NSDate date] dateWithYMD];
    
    // 2014-04-30
    NSDate *selfDate = [self dateWithYMD];
    
    // 获得nowDate和selfDate的差距
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *cmps = [calendar components:NSCalendarUnitDay fromDate:selfDate toDate:nowDate options:0];
    return cmps.day == 2;
- (NSDate *)dateWithYMD
{
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    fmt.dateFormat = @"yyyy-MM-dd";
    NSString *selfStr = [fmt stringFromDate:self];
    return [fmt dateFromString:selfStr];
}

一次不写多了,上次写了很多,居然没有保存,烦死了~~~~!!!

相关文章

  • iOS时间戳与日历大全

    1,是否为今天 2,是否为昨天 3,是否为前天 一次不写多了,上次写了很多,居然没有保存,烦死了~~~~!!!

  • iOS日历

    iOS日历:获取昨天, 一周前的今天 ,两周前的今天 ,上个月最后一天 iOS 13位(毫秒)时间戳输出

  • iOS- 时间和时间戳及与字符串转化

    iOS- 时间和时间戳及与字符串转化 1.获取当前时间 2.获取当前时间戳 3.时间戳转时间 4.字符串转时间戳 ...

  • time模块

    时间戳 生成当前时间戳 时间戳转日历时间 时间戳转时间元组 方法一 方法二 时间元组 定义时间元组 时间元组的每一...

  • 关于UI事件传递,图像显示,性能优化,离屏渲染

    更多的文章请看-2020iOS面试大全 持续更新! 2020iOS面试大全 UIView与CALayer 事件传递...

  • iOS 获取时间字符串与时间戳

    获取当前时间 获取当前时间戳 时间戳转时间 字符串转时间戳 注意:iOS 返回的时间戳单位是秒。

  • ios开发启动图与图标的尺寸问题大全

    ios开发启动图与图标的尺寸问题大全

  • iOS 关于时间的一些操作

    1、基本时间日历 2、获取当前系统时间的时间戳 3、将某个时间转化成时间戳 4、将某个时间戳转化成 时间 5、获取...

  • iOS 关于时间的一些操作

    1、基本时间日历 2、获取当前系统时间的时间戳 3、将某个时间转化成时间戳 4、将某个时间戳转化成 时间 5、获取...

  • iOS 时间戳

    引言 iOS开发过程中会遇到很多时间转的问题。例如:当服务器端给我们一个时间戳的时候,我们需要把它转化成具体的时间...

网友评论

      本文标题:iOS时间戳与日历大全

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