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];
    }



  • 相关阅读:
    aria2
    Tomcat Manager Config
    selenium 入门(Java)
    java线上cpu、内存问题排查方法
    Java多线程知识点
    《Java7并发编程实战手册》读书笔记
    《Java并发编程的艺术》读书笔记
    centos7安装mysql
    Linux用户配置文件
    metasploit魔鬼训练营靶机环境搭建(第二章)
  • 原文地址:https://www.cnblogs.com/pangblog/p/3279762.html
Copyright © 2011-2022 走看看