Convert Date to String in IOS SwiftUI.

huangapple go评论71阅读模式
英文:

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())

huangapple
  • 本文由 发表于 2023年5月10日 14:16:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76215382.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定