-3

I Have two question : 1) I want to convert the JSON string into time . I am getting the JSON String but i am not able to convert it into Time. Following is my code :

HERE SHOULD BE YOUR JSON ["status": 200, "data": 15:55, "Message": In Time]

the "data" i am getting is in string want to convert the data into actual time

if let datetime = json["data"] as? String {
    print("on CLick of in_time BUtton : \(datetime)")
    let formattter = DateFormatter()
    formattter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
    formattter.date(from: datetime)
    print("form : \(formattter)")
    print("Date : \(datetime)")
}

2) I Have a Grace time and I want to Compare it with the server time which i will get through json How i can Achieve it Please help !!! grace time is 10:30

Fogmeister
  • 76,236
  • 42
  • 207
  • 306
Sandesh Zote
  • 23
  • 1
  • 8
  • 2
    Why are you using a full date format when your JSON clearly only contains the time with minutes precision? Simply set `dateFormat` to `HH:mm`. – Dávid Pásztor Apr 13 '18 at 10:43
  • Even after using that i am getting error when i print "form" `form : ` – Sandesh Zote Apr 13 '18 at 10:47
  • What exactly do you expect to see by printing `formatter`? – mag_zbc Apr 13 '18 at 10:49
  • and how i can compare the time with the grace time if it is before grace time or after grace time. because in Android i am using it as `datetime.before(gracetime)` what i can use here ? – Sandesh Zote Apr 13 '18 at 10:49
  • @SandeshZote what error are you getting? You can simply compare `Date` objects using the comparison operators, namely `datetime < gracetime`. You also should print `formatter`, that makes no sense, you should print `formatter.date(from: datetime)`. – Dávid Pásztor Apr 13 '18 at 10:51
  • in `formatter` i will get the formatted time ?? – Sandesh Zote Apr 13 '18 at 10:51
  • Also, have in mind that if you have set 12-hour format in your device's settings, you need to set the locale for your `formatter` – mag_zbc Apr 13 '18 at 10:51
  • the updated time i will get as `let time = formatter.date(from: datetime)` – Sandesh Zote Apr 13 '18 at 10:54
  • @mag_zbc can you explain me more ?? any device can have 12 hour format how i can use locale ? – Sandesh Zote Apr 13 '18 at 10:57

2 Answers2

0

Change formattter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss" to formattter.dateFormat = "HH:mm" as @Dávid Pásztor said

DionizB
  • 1,487
  • 2
  • 10
  • 20
  • He should also set the locale to `formattter.locale = Locale(identifier: "en_US_POSIX")` – Leo Dabus Apr 13 '18 at 11:01
  • `let time = formattter.date(from: datetime) print("form : \(String(describing: time))")` and getting the value as `form : Optional(2000-01-01 11:04:00 +0000)` – Sandesh Zote Apr 13 '18 at 11:05
  • you need to unwrap the optional result using `if let` instead of `let`. You need also to use date formatter to convert your date back to string. `formatter.string(from: time)` – Leo Dabus Apr 13 '18 at 11:34
  • @LeoDabus i am sorry i did not undestand – Sandesh Zote Apr 13 '18 at 11:56
  • if you print a date you will get UTC time representation. If you would like to see the local time you need to use date formatter. `if let time = formattter.date(from: datetime) {` and when printing `print(formatter.string(from: time))` – Leo Dabus Apr 13 '18 at 11:59
  • don't forget to set the date formatter locale to `Locale(identifier: "en_US_POSIX")` – Leo Dabus Apr 13 '18 at 12:00
  • Ok I got it ?? now i have to use this `time` and compare it with `grace time` – Sandesh Zote Apr 13 '18 at 12:09
  • is `grace time` a string or Date type? – DionizB Apr 13 '18 at 13:06
  • `gracetime` is a string – Sandesh Zote Apr 13 '18 at 13:32
  • convert it to date format as the time you got from JSON and then you can look here how to compare two dates https://stackoverflow.com/questions/39018335/swift-3-comparing-date-objects – DionizB Apr 13 '18 at 14:04
  • can i directly fetch the data as `let datetime = json["data"] as? Date` – Sandesh Zote Apr 16 '18 at 08:53
  • No, because json["data"] is a string type! – DionizB Apr 16 '18 at 09:31
  • can i send the data as date format only through backend itself ?? will it be possible and then fetch it as Date then i wont have to convert it to date format and then again string – Sandesh Zote Apr 16 '18 at 09:54
  • Normally time is returned as string in API responses and everyone here told you how to access it, so why are you insisting with this idea of date type? – DionizB Apr 16 '18 at 13:15
  • yes i do agree with everyone . i just got a thought . i am not implementing that . but wanted to know if we can do it as well . Thank you for helping @DionizB – Sandesh Zote Apr 16 '18 at 13:19
0

You can use like:

 if let strResp = json["data"]{
        let strDate = "\(strResp!)" //15:55
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "HH:mm" // Pass any format here you want according to your date response
        let convDate = dateFormatter.date(from: strDate)
        print(convDate!)
    }
Abhirajsinh Thakore
  • 1,806
  • 2
  • 13
  • 23