无法解析日期 [英] Unable to parse a date

查看:100
本文介绍了无法解析日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从Web服务获取以下格式的日期时间字符串:

I'm getting a datetime string in the following format back from a web service:

2014-08-22T15:00:00-04:00

2014-08-22T15:00:00-04:00

最初,我手动剥离了片段并将其重建为NSDate变量.那时我遇到的问题是当我检索日期的一天部分时,从00到04的小时数会给我前一天的时间.即使将本地时区应用于日期格式器,新日期的打印值也以+0000结尾,这是不正确的.

Originally, I manually stripped the pieces out and rebuilt it to an NSDate variable. The problem I ran into then was when I retrieved the day part of the date, hours 00 through 04 would give me the previous day. Even when applying the local timezone to the date formatter, the printed value of the new date ended with +0000, which can't be right.

无论如何,我随后在Ojbective-C的Apple网站上找到了一些代码,并将其翻译为Swift:

Anyway, I then found some code on apple's site in Ojbective-C and translated it into Swift:

    func userVisibleDateTimeStringForRFC3339DateTimeString(rfc3339DateTimeString: String) -> NSDate {

        let rfc3339DateFormatter = NSDateFormatter()
        let enUSPOSIXLocale = NSLocale(localeIdentifier: "en_US_POSIX")
        rfc3339DateFormatter.locale = enUSPOSIXLocale
        rfc3339DateFormatter.dateFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"
        let date = rfc3339DateFormatter.dateFromString(rfc3339DateTimeString)


        return date!
    }

(原始代码产生了用户可读的日期字符串."我只需要日期.

(the original code produced a "user readable date string." I just needed the date.

现在,由于date为零,它在return语句上崩溃,这意味着它没有解析该datetime字符串.

Now, it crashes on the return statement because date is nil, meaning it's not parsing that datetime string.

我什至尝试过:

let format="yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"
var dateFmt = NSDateFormatter()

dateFmt.dateFormat = format

println(noaaDate)
let newreadableDate = dateFmt.dateFromString(noaaDate)
println(newreadableDate)

newizableDate为零

newreadableDate is nil

我需要做的就是将该字符串转换为一个日期,以便以后可以进行正确的日分量比较.

All I need is to convert that string into a date that will let me do proper day component comparisons later on.

推荐答案

请勿转义格式字符,不得转义'Z',它是格式语法字符.只有"T"必须转义,因为它是一种语法字符,不应解释为一个.-"和"="不是语法字符,因此不需要进行转义,而是可以转义.

Don't escape formatting characters, the 'Z' must not be escaped, it is a formatting syntax character. Only the 'T' must be escaped because it is a syntax character and should not be interpreted as one. The '-' and '=' are not syntax characters so they do not need to be escaped but can be.

在文档中:保留从a到z和A到Z的ASCII字母作为语法字符".
因此,除非将a-zA-Z字符中的任何一个转义,否则都将视为语法字符.

From the docs: "ASCII letter from a to z and A to Z are reserved as syntax characters".
So any of the characters a-zA-Z are considered syntax characters unless they are escaped.

let format ="yyyy-MM-dd'T'HH:mm:ssZ"

let format="yyyy-MM-dd'T'HH:mm:ssZ"

请参阅: ICU格式化日期和时间

let noaaDate = "2014-08-22T15:00:00-04:00"
let format="yyyy-MM-dd'T'HH:mm:ssZ"

var dateFmt = NSDateFormatter()
dateFmt.dateFormat = format
let newreadableDate = dateFmt.dateFromString(noaaDate)
println(newreadableDate)

输出:

可选(2014-08-22 19:00:00 +0000)

Optional(2014-08-22 19:00:00 +0000)

这篇关于无法解析日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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