如何在 Swift iOS 中将字符串转换为日期到字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33277970/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 08:03:50  来源:igfitidea点击:

How to convert string to date to string in Swift iOS?

iosstringswiftnsdatensdateformatter

提问by Yuvaraj.M

Am learning swift and am struck in converting the date String to NSDate to string. Am getting the date string in this format "Thu, 22 Oct 2015 07:45:17 +0000". I need to show the date in the MM-dd-yyyy format. I tried the following code but, it returns "null".

我学得很快,并且在将日期字符串转换为 NSDate 到字符串时感到震惊。我正在以这种格式获取日期字符串“2015 年 10 月 22 日星期四 07:45:17 +0000”。我需要以 MM-dd-yyyy 格式显示日期。我尝试了以下代码,但它返回“null”。

let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "MM-dd-yyyy"
dateFormatter.dateStyle = NSDateFormatterStyle.MediumStyle
let dateObj = dateFormatter.dateFromString(dateString!)
print("Dateobj: \(dateObj)")

Can anyone please help where am going wrong? Looking forward the help. Thanks in advance.

任何人都可以请帮助我哪里出错了?期待帮助。提前致谢。

回答by t4nhpt

First, you need to convert your string to NSDate with its format. Then, you change the dateFormatterto your simple format and convert it back to a String.

首先,您需要将字符串转换为 NSDate 及其格式。然后,将 更改dateFormatter为简单格式并将其转换回字符串。

Swift 3

斯威夫特 3

let dateString = "Thu, 22 Oct 2015 07:45:17 +0000"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "EEE, dd MMM yyyy hh:mm:ss +zzzz"
dateFormatter.locale = Locale.init(identifier: "en_GB")

let dateObj = dateFormatter.date(from: dateString)

dateFormatter.dateFormat = "MM-dd-yyyy"
print("Dateobj: \(dateFormatter.string(from: dateObj!))")

The printed result is: Dateobj: 10-22-2015

打印结果为: Dateobj: 10-22-2015

回答by Kishor Kumar Rawat

//String to Date Convert

var dateString = "2014-01-12"
var dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
let s = dateFormatter.dateFromString(dateString)
println(s)


//CONVERT FROM NSDate to String  

let date = NSDate()
var dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd" 
var dateString = dateFormatter.stringFromDate(date)
println(dateString)  

回答by brockk

Swift 2 and below

Swift 2 及以下

let date = NSDate()
var dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "MM-dd-yyyy"
var dateString = dateFormatter.stringFromDate(date)
println(dateString)

And in Swift 3 and higher this would now be written as:

而在 Swift 3 及更高版本中,这将被写为:

let date = Date()
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MM-dd-yyyy"
var dateString = dateFormatter.string(from: date)

回答by Alexey Pichukov

See answer from Gary Makin. And you need change the format or data. Because the data that you have do not fit under the chosen format. For example this code works correct:

请参阅 Gary Makin 的回答。并且您需要更改格式或数据。因为您拥有的数据不适合所选格式。例如,此代码工作正常:

let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "MM-dd-yyyy"
let dateObj = dateFormatter.dateFromString("10 10 2001")
print("Dateobj: \(dateObj)")