我想将字符串日期(“ 2019-06-29”)转换为格式为(“ yyyy-MM-dd”)的日期对象

时间:2019-05-29 07:16:52

标签: ios swift date nsdateformatter dateformatter

我想将字符串日期(“ 2019-06-28”)转换为日期对象(格式为“ yyyy-MM-dd”)。我的输出是2019-06-28 18:30:00 +0000,但是我不需要18:30:00 +0000。我只想要日期(2019-06-28)。

override func viewDidLoad() {
        super.viewDidLoad()

        let dateString = "2019-06-28"
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "yyyy-MM-dd"

        let date = dateFormatter.date(from: dateString)
        print(date!)


    }

1 个答案:

答案 0 :(得分:0)

尝试..

let dateString = "2019-06-28"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
let date = dateFormatter.date(from: dateString)
let eventDateFormat2 = DateFormatter()
eventDateFormat2.dateFormat = "yyyy-MM-dd"
let dateString2 = eventDateFormat2.string(from: date!)
print(dateString2)