在 Swift 中从字符串到 NSDate

2023-03-20移动开发问题
1

本文介绍了在 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

通过游戏中心发送 NSString
Send NSString via Game Center(通过游戏中心发送 NSString)...
2024-08-12 移动开发问题
5

“‘NSData’没有可见的@interface 声明选择器‘base64EncodedString’"MKS
quot;No visible @interface for #39;NSData#39; declares the selector #39;base64EncodedStringquot; error in MKStoreKit(“‘NSData’没有可见的@interface 声明选择器‘base64EncodedString’MKStoreKit 中的错误)...
2024-08-12 移动开发问题
3

使用 NSString 制作变量名
Make variable name out of NSString(使用 NSString 制作变量名)...
2024-08-11 移动开发问题
3

更新 Cocos2d 中的标签值
Update Label Value in Cocos2d(更新 Cocos2d 中的标签值)...
2024-08-11 移动开发问题
9

如何在 ios (Cocos2d) 中将字符串转换为音频(声音)文件?
How to convert String to Audio (Sound) file in ios (Cocos2d)?(如何在 ios (Cocos2d) 中将字符串转换为音频(声音)文件?)...
2024-08-11 移动开发问题
3

无法将文件从捆绑包复制到 iOS 中的文档目录
Can`t copy file from bundle to documents directory in iOS(无法将文件从捆绑包复制到 iOS 中的文档目录)...
2024-04-15 移动开发问题
4