zoukankan      html  css  js  c++  java
  • 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];
    }



  • 相关阅读:
    员工看公司
    Java多线程
    Intellij热部署插件JRebel的详细配置及图解
    Java并发处理锁 Lock
    Java8 Stream
    Eclipse下Maven安装和配置
    IntelliJ IDEA 部署Tomcat及创建一个web工程
    IntelliJ IDEA 下搭建vue项目工程
    Vue Router路由管理器介绍
    用WebStorm搭建vue项目
  • 原文地址:https://www.cnblogs.com/pangblog/p/3279762.html
Copyright © 2011-2022 走看看