我如何从日期转换此日期时间?
由此:2016-02-29 12:24:26 致:2016年2月29日
到目前为止,这是我的代码,它返回一个nil值:
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "MM-dd-yyyy"
dateFormatter.timeZone = NSTimeZone(name: "UTC")
let date: NSDate? = dateFormatter.dateFromString("2016-02-29 12:24:26")
print(date)
您必须声明2个不同的NSDateFormatters
,第一个将字符串转换为NSDate
,第二个以您的格式打印日期。
试试这段代码:
let dateFormatterGet = NSDateFormatter()
dateFormatterGet.dateFormat = "yyyy-MM-dd HH:mm:ss"
let dateFormatterPrint = NSDateFormatter()
dateFormatterPrint.dateFormat = "MMM dd,yyyy"
let date: NSDate? = dateFormatterGet.dateFromString("2016-02-29 12:24:26")
print(dateFormatterPrint.stringFromDate(date!))
Swift 3及更高版本:
从Swift 3开始,NSDate
等级已经变为Date
和NSDateFormatter
到DateFormatter
。
let dateFormatterGet = DateFormatter()
dateFormatterGet.dateFormat = "yyyy-MM-dd HH:mm:ss"
let dateFormatterPrint = DateFormatter()
dateFormatterPrint.dateFormat = "MMM dd,yyyy"
if let date = dateFormatterGet.date(from: "2016-02-29 12:24:26") {
print(dateFormatterPrint.string(from: date))
} else {
print("There was an error decoding the string")
}
只需使用以下函数转换日期格式: -
let dateFormat1 = DateFormatter()
dateFormat1.dateFormat = "EEEE"
let stringDay = dateFormat1.string(from: Date())
let dateFormat2 = DateFormatter()
dateFormat2.dateFormat = "MMMM"
let stringMonth = dateFormat2.string(from: Date())
let dateFormat3 = DateFormatter()
dateFormat3.dateFormat = "dd"
let numDay = dateFormat3.string(from: Date())
let stringDate = String(format: "%@\n%@ %@", stringDay.uppercased(), stringMonth.uppercased(), numDay)
如果要解析“1996-12-19T16:39:57-08:00”中的日期,请使用以下格式“yyyy-MM-dd'T'HH:mm:ssZZZZZ”:
let dateFormat = DateFormatter()
dateFormat.dateFormat = "EEEE\nMMMM dd"
let stringDate = dateFormat.string(from: Date()).uppercased()
来自Apple let convertedFormat = convertToString(dateString: "2019-02-12 11:23:12", formatIn: "yyyy-MM-dd hh:mm:ss", formatOut: "MMM dd, yyyy") //calling function
print(convertedFormat) // feb 12 2019
func convertToString (dateString: String, formatIn : String, formatOut : String) -> String {
let dateFormater = DateFormatter()
dateFormater.timeZone = NSTimeZone(abbreviation: "UTC") as TimeZone!
dateFormater.dateFormat = formatIn
let date = dateFormater.date(from: dateString)
dateFormater.timeZone = NSTimeZone.system
dateFormater.dateFormat = formatOut
let timeStr = dateFormater.string(from: date!)
return timeStr
}
迅捷3
let RFC3339DateFormatter = DateFormatter()
RFC3339DateFormatter.locale = Locale(identifier: "en_US_POSIX")
RFC3339DateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZZ"
RFC3339DateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
/* 39 minutes and 57 seconds after the 16th hour of December 19th, 1996 with an offset of -08:00 from UTC (Pacific Standard Time) */
let string = "1996-12-19T16:39:57-08:00"
let date = RFC3339DateFormatter.date(from: string)
将它放在扩展名中并像下面一样调用它。它在整个应用程序中都很容易使用。
https://developer.apple.com/documentation/foundation/dateformatter
履行
func dataFormat(dataJ: Double) -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .long
dateFormatter.timeStyle = .none
let date = Date(timeIntervalSince1970: dataJ)
return (dataJ != nil) ? "Today, \(dateFormatter.string(from: date))" : "Date Invalid"
}
这可能对想要使用dateformatter.dateformat的人有用;如果你想要qazxsw poi你使用qazxsw poi
12.09.18
dateformater.dateformat = "dd.MM.yy"
当您想要本地化您的应用程序时,这也很有用。 Locale(标识符:)使用Wednesday, Sep 12, 2018 --> EEEE, MMM d, yyyy
09/12/2018 --> MM/dd/yyyy
09-12-2018 14:11 --> MM-dd-yyyy HH:mm
Sep 12, 2:11 PM --> MMM d, h:mm a
September 2018 --> MMMM yyyy
Sep 12, 2018 --> MMM d, yyyy
Wed, 12 Sep 2018 14:11:54 +0000 --> E, d MMM yyyy HH:mm:ss Z
2018-09-12T14:11:54+0000 --> yyyy-MM-dd'T'HH:mm:ssZ
12.09.18 --> dd.MM.yy
10:41:02.112 --> HH:mm:ss.SSS
。另见let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .medium
dateFormatter.timeStyle = .none
dateFormatter.locale = Locale(identifier: "en_US")
print(dateFormatter.string(from: date)) // Jan 2, 2001
2018-02-02 06:50:16 +0000转换02-02-2018
let dateFormatterGet = DateFormatter()
dateFormatterGet.dateFormat = "yyyy-MM-dd HH:mm:ss"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MMM dd,yyyy"
let date: Date? = dateFormatterGet.date(from: "2017-02-14 17:24:26")
print(dateFormatter.string(from: date!))
2018-02-01T19:10:04 + 00:00转换时间:01年01月01日
mitul-nakum
迅捷3
extension Date {
static func getFormattedDate(string: String) -> String{
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss +zzzz" // This formate is input formated .
let formateDate = dateFormatter.date(from:"2018-02-02 06:50:16 +0000")!
dateFormatter.dateFormat = "dd-MM-yyyy" // Output Formated
print ("Print :\(dateFormatter.string(from: formateDate))")//Print :02-02-2018
return dateFormatter.string(from: formateDate)
}
}
我解决了问题的格式extension Date {
static func getFormattedDate(string: String , formatter:String) -> String{
let dateFormatterGet = DateFormatter()
dateFormatterGet.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
let dateFormatterPrint = DateFormatter()
dateFormatterPrint.dateFormat = "MMM dd,yyyy"
let date: Date? = dateFormatterGet.date(from: "2018-02-01T19:10:04+00:00")
print("Date",dateFormatterPrint.string(from: date!)) // Feb 01,2018
return dateFormatterPrint.string(from: date!);
}
}
(例如2018-06-15T00:00:00.000Z):
let date : Date = Date()
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MMM dd, yyyy"
let todaysDate = dateFormatter.string(from: date)
Swift 3带有yyyy-MM-dd'T'HH:mm:ss.SSS'Z'
扩展名
func formatDate(date: String) -> String {
let dateFormatterGet = DateFormatter()
dateFormatterGet.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .medium
dateFormatter.timeStyle = .none
// dateFormatter.locale = Locale(identifier: "en_US") //uncomment if you don't want to get the system default format.
let dateObj: Date? = dateFormatterGet.date(from: date)
return dateFormatter.string(from: dateObj!)
}
然后你可以像这样使用它:
Date
要将2016-02-29 12:24:26转换为日期,请使用此日期格式化程序:
extension Date {
func string(with format: String) -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = format
return dateFormatter.string(from: self)
}
}
编辑:要获得输出2016年2月29日使用此日期格式化程序:
let date = Date()
date.string(with: "MMM dd, yyyy")