• NSDate conversion utilities


    // Gets UTC NSDate from DateTime(.Net/WCF).

     

    + (NSDate *)fromDateTime:(NSString *)dateTime {
        NSDate *utcDate;
        if ([dateTime isMemberOfClass:[NSNull class]]) {
            utcDate = nil;
        } else {
            NSInteger offset = [[NSTimeZone timeZoneWithName:@"UTC"] secondsFromGMT];
            utcDate = [[NSDate dateWithTimeIntervalSince1970:[[dateTime substringWithRange:NSMakeRange(6, 10)] intValue]] dateByAddingTimeInterval:offset];
        }
        
        return utcDate;
    }

    // Converts NSDate to UTC DateTime(.Net/WCF).

    - (NSString *)toDateTime {
        NSDateFormatter *utcFormatter = [[NSDateFormatter alloc] init];
        [utcFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
        
        return [NSString stringWithFormat:@"/Date(%.0f000%@)/", [self timeIntervalSince1970],[utcFormatter stringFromDate:self]];
    }


     

    // Converts UTC NSDate to local time.

    - (NSString *)utcToLocalTime {
        // offset second
        NSInteger seconds = [[NSTimeZone systemTimeZone] secondsFromGMT];
        
        NSDateFormatter *localTimeFormatter = [[NSDateFormatter alloc] init];
        [localTimeFormatter setDateFormat:@"MM/dd/yyyy HH:mm"];
        [localTimeFormatter setTimeZone :[NSTimeZone timeZoneForSecondsFromGMT: seconds]];
        
        return [localTimeFormatter stringFromDate:self];
    }



  • 相关阅读:
    内部类
    多重继承关系初始化顺序及初始化
    String
    Error
    算法:插入排序
    算法:冒泡排序
    算法:选择排序
    注册Activity
    java变量的作用域和基本数据类型转换
    java数据类型
  • 原文地址:https://www.cnblogs.com/pangblog/p/3279762.html
Copyright © 2020-2023  润新知