适用于datetime的NSDateFormatter [英] NSDateFormatter for datetime

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

问题描述

我有一个来自API的字符串,我需要将其转换为 NSDate ,但我不确定哪个dateFormat用于 NSDateFormatter

I have a string which is coming from an API that I need to convert into a NSDate but I'm uncertain which dateFormat to use for NSDateFormatter.

let openedAt = "2015-06-30T12:34:00.000-04:00" // coming from API

let formatter = NSDateFormatter()
formatter.dateFormat = "yyyy-MM-dd..." // not sure what format to use here

if let date = formatter.dateFromString(openedAt) {
    println(date)
} else {
    println("NOPE!")
}


推荐答案

尝试

 "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"

日期格式模式指南表明 S是分数秒的格式说明符。

The Date Format Patterns guide suggests that "S" is the format specifier for fractions of seconds.

这篇关于适用于datetime的NSDateFormatter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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