convert string to date but output wrong date

58 Views Asked by At

I convert string to date but output wrong date. I change format date from json (30/12/2019) by replace / to - symbol (30-12-2019) But value after convert string to date show output 23-12-2018 output wrong date why date change from 30-12-2019 to be is 23-12-2018 How can I solve this issue?

value from json

self.FormattPlandate = dictionary["PlanDate"]! as? String 
print("FormattPlandate ==>\(String(describing: self.FormattPlandate))”). 

*//output 30/12/2019

For use in picker view

self.FormattpPlandateCategories = [self.getFormattDateTimes(textdate: self.FormattPlandate!)]

*// output wrong date 23-12-2018

func getFormattDateTimes(textdate: String)-> String{
//      let nowDate = textdate
        print("formatter now from Json ==> \(nowDate)")

        let formatter = DateFormatter()
        formatter.locale = Locale(identifier: "en_US")
        formatter.timeZone = TimeZone(abbreviation: "UTC+7")

//      replace / symbol to - symbol
        let newDate: String = nowDate.replacingOccurrences(of: "/", with: "-")
        formatter.dateFormat = "dd-MM-YYYY"
        print("formatter now from Json replace / symbol to - symbol ==> \(newDate)")

//      parse string to date
        formatter.dateFormat = "dd-MM-YYYY"
        let yourDate:Date = formatter.date(from: newDate)!
        print("formatter yourDate from Json Result ==> \(formatter.string(from: yourDate))") —>  why  date change from  30-08-2019  to  be 23-12-2018

//      set format for display
        formatter.dateFormat = "MMMM, dd YYYY"
        print("formatter showDate from json Result ==> \(formatter.string(from: yourDate))")  —> output wrong date 23 Dec 2018 

//  Equal Value For use in pickerview
        getpPlanDatePickerView.text = formatter.string(from: yourDate)
        getpShipmentPickerView.text = formatter.string(from: yourDate)
        FormattpPlandateCategories = [formatter.string(from: yourDate)]
        FormattgetpPlandateDCPlanData = [formatter.string(from: yourDate)]

        let text = newDate
        return text.uppercased()
   }
Output
formatter now from Json ==> 30/12/2019
formatter now from Json replace / symbol to - symbol ==> 30-12-2019
formatter yourDate from Json Result  after parse string to date ==> 23-12-2018
formatter showDate from Json Result  after parse string to date ==> December, 23 2018
0

There are 0 best solutions below