在 Swift 中从字符串到 NSDate

From String to NSDate in Swift(在 Swift 中从字符串到 NSDate)
本文介绍了在 Swift 中从字符串到 NSDate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有一个包含 datestring,格式如下:2014 年 9 月 4 日星期四 10:50:12 +0000

I've got a string containing a date in this format: Thu, 04 Sep 2014 10:50:12 +0000

您知道如何将其转换为:2014 年 4 月 9 日(dd-MM-yyyy)

Do you know how can I convert it to: 04-09-2014 (dd-MM-yyyy)

当然,使用 Swift

推荐答案

如果上述方法不起作用,这应该可以解决问题:

If the above doesn't work, this should do the trick:

let formatter = DateFormatter()
formatter.locale = Locale(identifier: "US_en")
formatter.dateFormat = "E, dd MMM yyyy HH:mm:ss Z"
let date = formatter.date(from: "Thu, 04 Sep 2014 10:50:12 +0000")

这篇关于在 Swift 中从字符串到 NSDate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Send NSString via Game Center(通过游戏中心发送 NSString)
quot;No visible @interface for #39;NSData#39; declares the selector #39;base64EncodedStringquot; error in MKStoreKit(“‘NSData’没有可见的@interface 声明选择器‘base64EncodedString’MKStoreKit 中的错误)
Make variable name out of NSString(使用 NSString 制作变量名)
Can`t copy file from bundle to documents directory in iOS(无法将文件从捆绑包复制到 iOS 中的文档目录)
How to copy a quot;Dictionaryquot; in Swift?(如何复制“字典在斯威夫特?)
What is the difference between quot;copyquot; and quot;retainquot;?(“复制和“复制有什么区别?和“保留?)