0

I have a time string only "17:10:51.2800000" want to change in HH:mm, but unable to convert it. I follow this code:

            let date = self.convertStringToDate(getDate: time!)
        
        let dateString = self.convertDateToString(getDate: date)


   func convertDateToString(getDate: Date) -> String {
        let formatter = DateFormatter()
        formatter.dateFormat = "yyyy-MM-dd HH:mm:ss.SSSSSSS"
        formatter.timeZone = NSTimeZone(name: "UTC") as TimeZone?
        let myString = formatter.string(from: getDate)
        let yourDate = formatter.date(from: myString)
        formatter.dateFormat = "HH:mm"
        formatter.timeZone = TimeZone.current
        let dateInString = formatter.string(from: yourDate!)
        return dateInString
    }
    
    func convertStringToDate(getDate: String) -> Date {
        let formatter = DateFormatter()
        formatter.setLocalizedDateFormatFromTemplate("HH:mm:ss.SSS")
        let dateInString = formatter.date(from: getDate)
        
        formatter.timeZone = NSTimeZone(name: "UTC") as TimeZone?//NSTimeZone(name: "UTC") as TimeZone?
       ////////here dateInString is coming nil while debugging
        return dateInString! //?? Date()
    }

While debugging convertStringToDate showing nil, but not crashing. I am confused it is correct or not.The time is coming correct after changing to string but not conform my code is correct or not.

  • If you want just `17:10` why not `let dateString = time.prefix(5)`? – vadian Dec 30 '20 at 15:13
  • `formatter.setLocalizedDateFormatFromTemplate("HH:mm:ss.SSS")` Your template is wrong. It will ignore any character that it is not a date component and/or its order. What you should pass is just `"Hm"` if you want a date format with 24h time only. Btw You should use this formatter to convert your parsed date to your desired time/string – Leo Dabus Dec 30 '20 at 15:19
  • Why are you using `setLocalizedDateFormatFromTemplate`, will the format of `time` depend on the users Locale? – Joakim Danielson Dec 30 '20 at 15:20
  • @JoakimDanielson and also he is passing a dateFormat instead of a template – Leo Dabus Dec 30 '20 at 15:21
  • @JoakimDanielson I think that OP issue is the device set to 12 hour format. OP is not setting the locale to "en_US_POSIX" before setting the dateFormat – Leo Dabus Dec 30 '20 at 15:24
  • possible duplicate of https://stackoverflow.com/a/40702569/2303865 – Leo Dabus Dec 30 '20 at 15:25
  • Note also that while debugging there is a bug that your optional date will show as nil. If you print it you will realise that it is not nil. – Leo Dabus Dec 30 '20 at 15:26
  • **"here dateInString is coming nil while debugging"** possible duplicate of https://stackoverflow.com/a/58148074/2303865 as well – Leo Dabus Dec 30 '20 at 15:57
  • thanks for your reply, my time string is "17:10:51.2800000" that is UTC, after changing to current it should differ by 5 hours, but its not coming. – user3120670 Dec 30 '20 at 16:00
  • @user3120670 What do you mean by "its not coming"? Have you set your locale to `"en_US_POSIX"` before setting the `dateFormat` property? Have you tried printing the resulting date)? You can not trust the debugging in this case. Btw there is no need to cast from `NSTimeZone` to `TimeZone`. You can simply use `TimeZone(secondsFromGMT: 0)!` and you should set the timezone before parsing your date. – Leo Dabus Dec 30 '20 at 16:17

0 Answers0