如何在Swift 3中设置label.text当前日期?
我想今天打印到屏幕上.我没有找到怎么做.
在c#中非常简单:
var date = DateTime.Now
Run Code Online (Sandbox Code Playgroud)
我需要在swift 3中写15.09.2016.谢谢
aya*_*aio 264
你在评论中说你想得到"15.09.2016".
为此,使用Date和DateFormatter:
let date = Date()
let formatter = DateFormatter()
Run Code Online (Sandbox Code Playgroud)
将格式您想要的格式提供给格式化程序:
formatter.dateFormat = "dd.MM.yyyy"
Run Code Online (Sandbox Code Playgroud)
获取结果字符串:
let result = formatter.string(from: date)
Run Code Online (Sandbox Code Playgroud)
设置标签:
label.text = result
Run Code Online (Sandbox Code Playgroud)
结果:
2016年9月15日
Jor*_*ego 60
你可以用Swift 3.0这样做:
let date = Date()
let calendar = Calendar.current
let components = calendar.dateComponents([.year, .month, .day], from: date)
let year = components.year
let month = components.month
let day = components.day
print(year)
print(month)
print(day)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
164827 次 |
| 最近记录: |