如何在 Swift 中以短格式获取当前日期 [英] How do I get the current Date in short format in Swift
本文介绍了如何在 Swift 中以短格式获取当前日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在下图中你可以看到我写的代码和所有变量的值:
In the images below you can see the code I wrote and the values of all the variables:
class fun getCurrentShortDate() -> String {
var todaysDate = NSDate()
var dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy"
var DateInFormat = dateFormatter.stringFromDate(todaysDate)
return DateInFormat
}
如您所见,找到当前日期没有问题,但是当我尝试将 NSDate 更改为字符串时,它不会这样做.
As you can see the current date is found no problem, but when I try to change the NSDate to a string, it just won't do it.
推荐答案
Xcode 11 或更高版本 • Swift 5.1 或更高版本
extension TimeZone {
static let gmt = TimeZone(secondsFromGMT: 0)!
}
extension Formatter {
static let date = DateFormatter()
}
<小时>
extension Date {
func localizedDescription(dateStyle: DateFormatter.Style = .medium,
timeStyle: DateFormatter.Style = .medium,
in timeZone : TimeZone = .current,
locale : Locale = .current) -> String {
Formatter.date.locale = locale
Formatter.date.timeZone = timeZone
Formatter.date.dateStyle = dateStyle
Formatter.date.timeStyle = timeStyle
return Formatter.date.string(from: self)
}
var localizedDescription: String { localizedDescription() }
}
<小时>
Date().localizedDescription // "Sep 26, 2018 at 12:03:41 PM"
Date().localizedDescription(in: .gmt) // "Sep 26, 2018 at 3:03:41 PM" UTC TIME
Date().localizedDescription(dateStyle: .short, timeStyle: .short) // "9/26/18, 12:03 PM"
Date().localizedDescription(dateStyle: .full, timeStyle: .full) // "Wednesday, September 26, 2018 at 12:03:41 PM Brasilia Standard Time"
Date().localizedDescription(dateStyle: .full, timeStyle: .full, in: .gmt) // "Wednesday, September 26, 2018 at 3:03:41 PM Greenwich Mean Time"
<小时>
extension Date {
var fullDate: String { localizedDescription(dateStyle: .full, timeStyle: .none) }
var longDate: String { localizedDescription(dateStyle: .long, timeStyle: .none) }
var mediumDate: String { localizedDescription(dateStyle: .medium, timeStyle: .none) }
var shortDate: String { localizedDescription(dateStyle: .short, timeStyle: .none) }
var fullTime: String { localizedDescription(dateStyle: .none, timeStyle: .full) }
var longTime: String { localizedDescription(dateStyle: .none, timeStyle: .long) }
var mediumTime: String { localizedDescription(dateStyle: .none, timeStyle: .medium) }
var shortTime: String { localizedDescription(dateStyle: .none, timeStyle: .short) }
var fullDateTime: String { localizedDescription(dateStyle: .full, timeStyle: .full) }
var longDateTime: String { localizedDescription(dateStyle: .long, timeStyle: .long) }
var mediumDateTime: String { localizedDescription(dateStyle: .medium, timeStyle: .medium) }
var shortDateTime: String { localizedDescription(dateStyle: .short, timeStyle: .short) }
}
<小时>
print(Date().fullDate) // "Friday, May 26, 2017
"
print(Date().shortDate) // "5/26/17
"
print(Date().fullTime) // "10:16:24 AM Brasilia Standard Time
"
print(Date().shortTime) // "10:16 AM
"
print(Date().fullDateTime) // "Friday, May 26, 2017 at 10:16:24 AM Brasilia Standard Time
"
print(Date().shortDateTime) // "5/26/17, 10:16 AM
"
这篇关于如何在 Swift 中以短格式获取当前日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文