英文:
Convert Date to String in IOS Swiftui
问题
当我打印所选日期时,我得到了
2023-05-19 18:32:00 +0000
如何将其转换为字符串,我只需要 2023-05-19
期望的输出是 "2023-05-19"
英文:
Date Picker :
DatePicker(selection: $vm.user.movingdate,
displayedComponents: [.date]) {
Label("Select a date", systemImage: "calendar")
}
when i print the selected date , i got
2023-05-19 18:32:00 +0000
how to convert it to String and i need only 2023-05-19
Expected Output is "2023-05-19"
答案1
得分: 0
我重新打开了被标记为重复的问题,因为两个链接的答案都没有涵盖SwiftUI方式来格式化日期
let date = Date()
let formattedDate = date.formatted(.iso8601.year().month().day())
英文:
I reopened the question which was marked as duplicate because both linked answers don't cover the SwiftUI-way to format the date
let date = Date()
let formattedDate = date.formatted(.iso8601.year().month().day())
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论