【发布时间】:2023-04-10 19:17:01
【问题描述】:
我正在寻找一种使用 Swift 3 在标签中显示当前日期的方法。 目前我正在使用 DateFormatter.dateFormat 但我得到的唯一输出是
dd.MM.yyyy
这是代码行:
lbl_Date.text = DateFormatter.dateFormat(fromTemplate: "MMddyyyy", options: 0, locale: NSLocale(localeIdentifier: "de-AT") as Locale)
我今天的预期输出应该是:
11.12.2016
我知道我可以使用 dateTimeComponents 获取时间,但我还没有弄清楚如何才能在标签中仅获取日期(日、月和年):
// get the current date and time
let currentDateTime = Date()
// get the user's calendar
let userCalendar = Calendar.current
// choose which date and time components are needed
let requestedComponents: Set<Calendar.Component> = [
.year,
.month,
.day,
.hour,
.minute,
.second
]
// get the components
let dateTimeComponents = userCalendar.dateComponents(requestedComponents, from: currentDateTime)
在我的标签中显示当前日期的最简洁方式是什么?
【问题讨论】:
-
那个功能是创建日期格式。然后用于将日期呈现为字符串。查看这数千个关于堆栈溢出的类似问题中的任何一个,以了解如何解决。
标签: swift nsdateformatter