如何在swift中获取日期

如何在swift中获取日期,swift,date,Swift,Date,是否有办法在swift中以“2019年5月17日”的格式获取日期,目前我有 var date = Date(timeIntervalSinceReferenceDate: (Date().timeIntervalSinceReferenceDate)) var stringDate = "\(date)" stringDate = String(stringDate.prefix(16)) 例如,它的格式为“2019-17-05 1:31”使用并设置日期样式(而不是日期格式)。您的第一行应该是

是否有办法在swift中以“2019年5月17日”的格式获取日期,目前我有

var date = Date(timeIntervalSinceReferenceDate: (Date().timeIntervalSinceReferenceDate))
var stringDate = "\(date)"
stringDate = String(stringDate.prefix(16))

例如,它的格式为“2019-17-05 1:31”

使用并设置
日期样式(而不是
日期格式
)。您的第一行应该是:
让日期=日期()
。您当然可以在中找到答案。我不建议在UI中显示日期时指定文本
日期格式。对本地化的日期字符串使用
dateStyle
。再次使用
let date=date()
let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "MMMM d, yyyy"
        var date = Date(timeIntervalSinceReferenceDate: (Date().timeIntervalSinceReferenceDate))
        print(dateFormatter.string(from: date))