swiftui datepicker를 사용하여 firebase 데이터베이스 날짜 및 시간을 밀리초 없이 표시하는 방법

Asked 1 years ago, Updated 1 years ago, 178 views

앱에 날짜와 시간을 초 및 밀리초 없이 표시하려고 합니다.

그래서 기본적으로 초 후 아래 이미지에 표시되는 내용을 표시합니다. (+0000 제거)

다음 코드로 날짜를 저장합니다.

VStack{

    DatePicker("Date", selection: $selectedDate)

                    .datePickerStyle(CompactDatePickerStyle())

                    .frame(height: 50)

}

 

데이타베이스의 데이터를 다음과 같이 읽습니다.

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)

 

firebase 데이터베이스에서 날짜를 읽고 있지만 현재 보기에는 생략하고 싶은 시간(밀리초)이 모두 표시됩니다. 

ios firebase date swiftui datepicker

2022-06-21 11:14

1 Answers

검색된 데이터가 이미 고정된 형식의 문자열에 있으므로 가장 쉬운 방법은 String.prefix를 사용하는 것입니다.

변수 날짜가 검색된 날짜를 고정 형식의 문자열로 저장하는 문자열 변수라고 가정합니다.

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


2022-06-21 11:15

If you have any answers or tips


© 2023 OneMinuteCode. All rights reserved.