目标

格式化日期显示(效果)

实现代码

import SwiftUI

struct DateFormat: View {
    
    var currentTime = Date()
    
    var body: some View {
            let formattedCurrentTime = getFormattedDate(format: "yyyy-MM-dd HH:mm:ss")
        
            return Text("\(formattedCurrentTime)")
                    .padding()
                    .font(.largeTitle)
                    .foregroundColor(.white)
                    .clipShape(Capsule())
                    .background(Color.blue)
                    .cornerRadius(15)
    }
    
    func getFormattedDate(format: String) -> String {
        let dateformat = DateFormatter()
        dateformat.dateFormat = format
        return dateformat.string(from: currentTime)
    }
}

struct DateFormat_Previews: PreviewProvider {
    static var previews: some View {
        DateFormat()
    }
}

欢迎单击 GitHub 或直接通过链接https://github.com/Findaaron/iOSDevelop/tree/master/iOSDemo/iOSDemo访问代码仓库。