分享

iOS 获取到当前时间后 如何叠加时间

 叹落花 2017-08-17
给你写个NSString方法(可以写在分类,也可以写在控制器直接调用),调用下面方法设置传入参数hour为2即可,其他参数为零。
注意看你本地是否处理了格林尼时区时间,如果处理了可以不用加8,看打印吧

/**
*  ** 在当前日期时间加上 某个时间段(传负数即返回当前时间之前x月x日的时间)
*
*  @param year   当前时间若干年后 (传负数为当前时间若干年前)
*  @param month  当前时间若干月后  (传0即与当前时间一样)
*  @param day    当前时间若干天后
*  @param hour   当前时间若干小时后
*  @param minute 当前时间若干分钟后
*  @param second 当前时间若干秒后
*
*  @return 处理后的时间字符串
*/
- (NSString *)dateStringAfterlocalDateForYear:(NSInteger)year Month:(NSInteger)month Day:(NSInteger)day Hour:(NSInteger)hour Minute:(NSInteger)minute Second:(NSInteger)second
{
    // 当前日期
    NSDate *localDate = [NSDate date]; // 为伦敦时间
    // 在当前日期时间加上 时间:格里高利历
    NSCalendar *gregorian = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *offsetComponent = [[NSDateComponents alloc]init];
    
    [offsetComponent setYear:year ];  // 设置开始时间为当前时间的前x年
    [offsetComponent setMonth:month];
    [offsetComponent setDay:day];
    [offsetComponent setHour:(hour+8)]; // 中国时区为正八区,未处理为本地,所以+8
    [offsetComponent setMinute:minute];
    [offsetComponent setSecond:second];
    // 当前时间后若干时间
    NSDate *minDate = [gregorian dateByAddingComponents:offsetComponent toDate:localDate options:0];
    
    NSString *dateString = [NSString stringWithFormat:@"%@",minDate];
    
    return dateString;

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多