在 iOS Swift 中将 NSDate 转换为字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42524651/
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
Convert NSDate to String in iOS Swift
提问by iOS
I am trying to convert a NSDate
to a String
and then Change Format. But when I pass NSDate
to String
it is producing whitespace.
我正在尝试将 a 转换NSDate
为 aString
然后更改格式。但是当我传递NSDate
给String
它时会产生空白。
let formatter = DateFormatter()
let myString = (String(describing: date))
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let yourDate: Date? = formatter.date(from: myString)
formatter.dateFormat = "dd-MMM-yyyy"
print(yourDate)
回答by Anbu.Karthik
you get the detail information from Apple Dateformatter Document.If you want to set the dateformat for your dateString, see this link, the detail dateformat you can get herefor e.g , do like
您可以从Apple Dateformatter Document获得详细信息。如果您想为 dateString 设置日期格式,请参阅此链接,您可以在此处获取详细日期格式, 例如,像
let formatter = DateFormatter()
// initially set the format based on your datepicker date / server String
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let myString = formatter.string(from: Date()) // string purpose I add here
// convert your string to date
let yourDate = formatter.date(from: myString)
//then again set the date format whhich type of output you need
formatter.dateFormat = "dd-MMM-yyyy"
// again convert your date to string
let myStringafd = formatter.string(from: yourDate!)
print(myStringafd)
you get the output as
你得到的输出为
回答by roy
I always use this code while converting Date
to String
. (Swift 3)
我在转换Date
为String
. (斯威夫特 3)
extension Date
{
func toString( dateFormat format : String ) -> String
{
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = format
return dateFormatter.string(from: self)
}
}
and call like this . .
并像这样调用。.
let today = Date()
today.toString(dateFormat: "dd-MM")
回答by Adrian
DateFormatter
has some factory date styles for those too lazy to tinker with formatting strings. If you don't need a custom style, here's another option:
DateFormatter
对于那些懒得修改格式化字符串的人,有一些工厂日期样式。如果您不需要自定义样式,这是另一种选择:
extension Date {
func asString(style: DateFormatter.Style) -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = style
return dateFormatter.string(from: self)
}
}
This gives you the following styles:
这为您提供了以下样式:
short, medium, long, full
Example usage:
用法示例:
let myDate = Date()
myDate.asString(style: .full) // Wednesday, January 10, 2018
myDate.asString(style: .long) // January 10, 2018
myDate.asString(style: .medium) // Jan 10, 2018
myDate.asString(style: .short) // 1/10/18
回答by Brijesh Shiroya
Your updated code.update it.
您更新的代码。更新它。
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let myString = formatter.string(from: date as Date)
let yourDate: Date? = formatter.date(from: myString)
formatter.dateFormat = "dd-MMM-yyyy"
print(yourDate!)
回答by Kevin Stewart
Something to keep in mind when creating formatters is to try to reuse the same instance if you can, as formatters are fairly computationally expensive to create. The following is a pattern I frequently use for apps where I can share the same formatter app-wide, adapted from NSHipster.
创建格式化程序时要记住的是,如果可以,请尝试重用相同的实例,因为创建格式化程序的计算成本相当高。以下是我经常用于应用程序的模式,我可以在应用程序范围内共享相同的格式化程序,改编自 NSHipster。
extension DateFormatter {
static var sharedDateFormatter: DateFormatter = {
let dateFormatter = DateFormatter()
// Add your formatter configuration here
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
return dateFormatter
}()
}
Usage:
用法:
let dateString = DateFormatter.sharedDateFormatter.string(from: Date())
回答by jignesh Vadadoriya
After allocating DateFormatter
you need to give the formatted string
then you can convert as string like this way
分配后,DateFormatter
您需要提供格式化的字符串,然后您可以像这样转换为字符串
var date = Date()
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let myString = formatter.string(from: date)
let yourDate: Date? = formatter.date(from: myString)
formatter.dateFormat = "dd-MMM-yyyy"
let updatedString = formatter.string(from: yourDate!)
print(updatedString)
OutPut
输出
01-Mar-2017
2017 年 3 月 1 日
回答by Arthur Stepanov
You can use this extension:
你可以使用这个扩展:
extension Date {
func toString(withFormat format: String) -> String {
let formatter = DateFormatter()
formatter.dateFormat = format
let myString = formatter.string(from: self)
let yourDate = formatter.date(from: myString)
formatter.dateFormat = format
return formatter.string(from: yourDate!)
}
}
And use it in your view controller like this (replace <"yyyy"> with your format):
并像这样在您的视图控制器中使用它(将 <"yyyy"> 替换为您的格式):
yourString = yourDate.toString(withFormat: "yyyy")