Home > Mobile >  swiftui datepicker how to display from firebase database date and time without milliseconds?
swiftui datepicker how to display from firebase database date and time without milliseconds?

Time:06-30

I'm trying to display the date and time on my app without the seconds and milli seconds, so basically what is shown on the bottom image after the seconds. (removing 0000) date how it is displaying I save the date with the following code:

         VStack{
                                DatePicker("Date", selection: $selectedDate)
                                             .datePickerStyle(CompactDatePickerStyle())
                                             .frame(height: 50)

             
                            }

and i'm reading the data from the database as:

func getData() {
    let db = Firestore.firestore()
    db.collection("clubs").getDocuments { snapshot, error in
        if error == nil {
            if let snapshot = snapshot {
                DispatchQueue.main.async{
                    
                    self.list = snapshot.documents.map { d in
                        return Clubs(id: d.documentID,
                                     clubID: d["clubID"] as? Int ?? 0,
                                     clubName: d["clubName"] as? String ?? "",
                                     clubCreator: d["clubCreator"] as? String ?? "",
                                     membersLimit: d["membersLimit"] as? Int ?? 0,
                                     streamingPlatforms: d["streamingPlatforms"] as? Int ?? 0,
                                     streamingType: d["streamingType"] as? Int ?? 0,
                                     teleClubGenres:d["teleClubGenres"] as? [String] ?? [],
                                     date:d["date"] as? String ?? "",
                                     description: d["description"] as? String ?? "", uid: d["uid"] as? String ?? "",
                                     currentMembers: d["currentMembers"] as? [String] ?? [],
                                     selectedDate: d["selectedDate"] as? String ?? ""
                        )
                    }}
            }
        }
        else{
        }
    }}


                                            
    Text(tele.selectedDate).frame(maxWidth: .infinity, alignment: .leading)

and i'm reading the date from the firebase database, but yah, currently my view shows everything including the milliseconds which i want to omit.

CodePudding user response:

Since your retrieved data is already in String with a fixed format, the easiest way to do it is using String.prefix.

Assuming variable date is the String variable which you store the retrieved date as String with a fixed format:

//before: "2022-06-15 23:18:00  0000"
let newDate = date.prefix(16)
//after: "2022-06-15 23:18"
  • Related