NSDate和NSDateFormatter 相关应用代码示例


此方法用来计算当前时间与目标时间的先后顺序:


-(NSDate *)calculateTimeWithCurrentTime:(NSDate *)currentDate{
    //将当前时间转为本地时区
    NSDate * currentLocalDate = [self localDateFromDate:currentDate];
    NSLog(@"currentDate--->%@",currentLocalDate);
    //设置目标时间为当天15:30
    NSString *dateStr = [NSString stringWithFormat:@"%@",currentLocalDate];
    NSString *dateString = [dateStr substringToIndex:11];
    NSString *targetDateStr = [NSString stringWithFormat:@"%@15:30:00",dateString];
    NSDateFormatter *formatter001 = [[NSDateFormatter alloc] init];
    [formatter001 setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    NSDate * targetDate = [formatter001 dateFromString:targetDateStr];
    //将目标时间转为本地时区
    targetDate = [self localDateFromDate:targetDate];
    NSLog(@"targetDate--->%@",targetDate);
    //比较当前时间和目标时间
    NSComparisonResult result = [currentLocalDate compare:targetDate];
    NSLog(@"result--->%d",result);
    //获取当前日期是星期几
    NSString *weekDay = [self weekdayStringFromDate:currentDate];
    NSLog(@"weekDay--->%@",weekDay);
    //计算收益时间
    NSDate *calDate;
    if ([weekDay isEqualToString:@"周一"] || [weekDay isEqualToString:@"周二"] || [weekDay isEqualToString:@"周三"] ) {
        //升序
        if (result == NSOrderedAscending) {
            NSLog(@"15:30之前");
            calDate = [currentDate dateByAddingTimeInterval:aDay];
        }
        else{
            NSLog(@"15:30之后");
            calDate = [currentDate dateByAddingTimeInterval:2*aDay];
        }
    }
    else if ([weekDay isEqualToString:@"周四"]){
        //升序
        if (result == NSOrderedAscending) {
            calDate = [currentDate dateByAddingTimeInterval:aDay];
        }
        else{
            calDate = [currentDate dateByAddingTimeInterval:4*aDay];
        }
    }
    else if ([weekDay isEqualToString:@"周五"]){
        //升序
        if (result == NSOrderedAscending) {
            calDate = [currentDate dateByAddingTimeInterval:3*aDay];
        }
        else{
            calDate = [currentDate dateByAddingTimeInterval:4*aDay];
        }
    }
    else if ([weekDay isEqualToString:@"周六"]){
        calDate = [currentDate dateByAddingTimeInterval:3*aDay];
    }
    else if ([weekDay isEqualToString:@"周日"]){
        calDate = [currentDate dateByAddingTimeInterval:2*aDay];
    }
    calDate = [self localDateFromDate:calDate];
    return calDate;
}

将时区转为当前时区

- (NSDate *)localDateFromDate:(NSDate *)date{
    //获取系统时区
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    //时间间隔
    NSInteger interval = [zone secondsFromGMTForDate: date];
    //得到当前时间
    NSDate *localeDate = [date dateByAddingTimeInterval: interval];
    return localeDate;
}

判断当天是星期几:

- (NSString*)weekdayStringFromDate:(NSDate*)inputDate { 
    NSArray *weekdays = [NSArray arrayWithObjects: [NSNull null],@"周日", @"周一", @"周二", @"周三", @"周四", @"周五", @"周六", nil];
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:@"Asia/Shanghai"];
    [calendar setTimeZone: timeZone];
    NSCalendarUnit calendarUnit = NSCalendarUnitWeekday;
    NSDateComponents *theComponents = [calendar components:calendarUnit fromDate:inputDate];
    return [weekdays objectAtIndex:theComponents.weekday];
}
优质内容筛选与推荐>>
1、Git提交引用和引用日志
2、树链剖分【洛谷P2590】 [ZJOI2008]树的统计
3、Codeforces 1093E Intersection of Permutations (CDQ分治+树状数组)
4、hdu 4848 搜索+剪枝 2014西安邀请赛
5、局域网中如何查看外网ip地址


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号