带有 NSDateFormatter 的 GMT 和 UTC 格式化程序 [英] GMT and UTC formatter with NSDateFormatter

查看:74
本文介绍了带有 NSDateFormatter 的 GMT 和 UTC 格式化程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了转换当前时间 utc-gmt 或 gmt -utc 的函数.如果 msgArrivedDate 为空,这些函数就可以正常工作.如果不是(这意味着,msgArrivedDate 来自不转换的休息服务.

JSON 解析部分:

NSArray *messageSentTime = [[args valueForKey:@"messageSendDate"] objectAtIndex:0];for(int i=0 ;i< [messageSentTime count]; i++){//[self timeZoneFormatter:@"GMT" :[messageSentTime objectAtIndex:i]];NSLog(@"转换时间 = %@",[self timeZoneFormatter:@"GMT" :[messageSentTime objectAtIndex:i]]);

功能部分:

-(id)timeZoneFormatter:(NSString *)formatType : (NSString *)msgArrivedDate{NSDate *日期;NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];if([msgArrivedDate 长度] > 0){date = [dateFormatter dateFromString:msgArrivedDate];} 别的 {日期 = [NSDate 日期];}if([formatType isEqualToString:@"UTC"]){[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];}if([formatType isEqualToString:@"GMT"]){[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];}NSString *dateString = [dateFormatter stringFromDate:date];返回日期字符串;}

ReST 以 UTC 格式返回这些值:

<块引用>

"2013-09-24 15:03:17","2013-09-25 12:09:22","2013-09-25 13:07:45","2013-09-25 13:08:19",2013-09-25 14:22:38"

当我调用该函数时,(NSLog(@"Converted time = %@",[self timeZoneFormatter:@"GMT" :[messageSentTime objectAtIndex:i]])) 返回:

 messageSentTime = ("2013-09-24 15:03:17","2013-09-25 12:09:22","2013-09-25 13:07:45","2013-09-25 13:08:19","2013-09-25 14:22:38")

我想我只是在这里遗漏了一个小问题:(还没有找到......

解决方案

改变这个:

[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

为此:

[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];

此方法将处理将日期字符串从 UTC 转换为 GMT 以及从 GMT 转换为 UTC:

- (id)translateDate:(NSString *)msgArrivedDate来自:(NSString *)fromTimeZoneto:(NSString *)toTimeZone {NSDate *date = nil;NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];if([fromTimeZone isEqualToString:@"UTC"]) {[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];}if([fromTimeZone isEqualToString:@"GMT"]) {[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];}if([msgArrivedDate 长度] > 0) {date = [dateFormatter dateFromString:msgArrivedDate];} 别的 {日期 = [NSDate 日期];}if([toTimeZone isEqualToString:@"UTC"]) {[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];}if([toTimeZone isEqualToString:@"GMT"]) {[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];}NSString *dateString = [dateFormatter stringFromDate:date];返回日期字符串;}

将2013-09-25 14:22:38"从UTC转换为GMT(即本地时间,在您使用GMT时)将导致2013-09-25 15:22:38,类似地,将2013-09-25 14:22:38"从 GMT 转换为 UTC 将导致 2013-09-25 13:22:38.

i wrote function that converting current time utc-gmt or gmt -utc. The functions just works fine if msgArrivedDate is null. If it's not ( that means , msgArrivedDate comes from rest service that doses not convert .

jSON parse part :

NSArray *messageSentTime = [[args valueForKey:@"messageSendDate"] objectAtIndex:0];

    for(int i=0 ;i< [messageSentTime count]; i++)
    {
        //[self timeZoneFormatter:@"GMT" :[messageSentTime objectAtIndex:i]];

        NSLog(@"Converted time = %@",[self timeZoneFormatter:@"GMT" :[messageSentTime objectAtIndex:i]]);

Function part :

-(id)timeZoneFormatter:(NSString *)formatType : (NSString *)msgArrivedDate
{
    NSDate *date;

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];


    if([msgArrivedDate length] > 0)
    {

        date = [dateFormatter dateFromString:msgArrivedDate];

    } else {

        date = [NSDate date];
    }


    if([formatType isEqualToString:@"UTC"])
    {
        [dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];

    }

    if([formatType isEqualToString:@"GMT"])
    {
        [dateFormatter setTimeZone:[NSTimeZone localTimeZone]];

    }

    NSString *dateString = [dateFormatter stringFromDate:date];

    return dateString;
}

ReST returned me to these values in UTC format :

"2013-09-24 15:03:17",
"2013-09-25 12:09:22",
"2013-09-25 13:07:45",
"2013-09-25 13:08:19",
"2013-09-25 14:22:38"

When i call the function, (NSLog(@"Converted time = %@",[self timeZoneFormatter:@"GMT" :[messageSentTime objectAtIndex:i]])) returns :

 messageSentTime = (
    "2013-09-24 15:03:17",
    "2013-09-25 12:09:22",
    "2013-09-25 13:07:45",
    "2013-09-25 13:08:19",
    "2013-09-25 14:22:38" )

I think i am just missing little issue over here :( couldn't find yet ...

解决方案

Change this:

[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

to this:

[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];

Edit: This method will handle translating the date string from UTC to GMT and from GMT to UTC:

- (id)translateDate:(NSString *)msgArrivedDate
               from:(NSString *)fromTimeZone
                 to:(NSString *)toTimeZone {
    NSDate *date = nil;
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

    if([fromTimeZone isEqualToString:@"UTC"]) {
        [dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
    }

    if([fromTimeZone isEqualToString:@"GMT"]) {
        [dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
    }

    if([msgArrivedDate length] > 0) {
        date = [dateFormatter dateFromString:msgArrivedDate];
    } else {
        date = [NSDate date];
    }

    if([toTimeZone isEqualToString:@"UTC"]) {
        [dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
    }

    if([toTimeZone isEqualToString:@"GMT"]) {
        [dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
    }

    NSString *dateString = [dateFormatter stringFromDate:date];
    return dateString;
}

Converting "2013-09-25 14:22:38" from UTC to GMT (i.e. local time, in your usage of GMT) will result in 2013-09-25 15:22:38, and similarly, converting "2013-09-25 14:22:38" from GMT to UTC will result in 2013-09-25 13:22:38.

这篇关于带有 NSDateFormatter 的 GMT 和 UTC 格式化程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆