将带有 T 和 Z 字符的时间戳字符串转换为 NSDate [英] Converting timestamp string with T and Z characters to NSDate

查看:54
本文介绍了将带有 T 和 Z 字符的时间戳字符串转换为 NSDate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何将此字符串转换为 NSDate.

I don't know how to convert this string to NSDate.

日期格式为2012-10-21T07:00:00Z"

Date is in this format "2012-10-21T07:00:00Z"

我想问题是我不知道字符串中的 T 和 Z 是什么意思:)

I guess the problem is that I have no clue what those T and Z mean in the string :)

NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:@"yyyy-MM-ddTHH:mm:ssZ"];
NSDate *dateStart = [formatter dateFromString:@"2012-10-21T07:00:00Z"];

// format that I want
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm"];

NSLog(@"date: %@", [formatter stringFromDate:dateStart]);

所有日期都采用该格式:2014-09-12T03:46:25Z

all dates are in that format: 2014-09-12T03:46:25Z

推荐答案

这是一个 ISO 日期.Z 代表祖鲁语,但表示 UTC 时间.

That is an ISO date. The Z stands for Zulu but means UTC time.

试试:

[formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZZZZZ"];

提供您的初始 nsdate.

To feed your initial nsdate.

这篇关于将带有 T 和 Z 字符的时间戳字符串转换为 NSDate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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