From a6713d3c40bef341700cddcbd919767bf8083c86 Mon Sep 17 00:00:00 2001 From: Bulut Gozubuyuk Date: Thu, 11 Apr 2024 14:13:34 -0400 Subject: [PATCH] Create CheckValidDate.swift (#5618) In this program, isValidDate function uses DateFormatter with the format "yyyy-MM-dd". The property isLenient is set to false to ensure the formatter does not adjust invalid dates to make them valid. For example, February 29, 2019, is an invalid date because 2019 is not a leap year, and this program will correctly output False for this input. Co-authored-by: Ritesh Kokam <61982298+RiteshK-611@users.noreply.github.com> --- .../program/check-valid-date/CheckValidDate.swift | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 program/program/check-valid-date/CheckValidDate.swift diff --git a/program/program/check-valid-date/CheckValidDate.swift b/program/program/check-valid-date/CheckValidDate.swift new file mode 100644 index 000000000..ed9aca725 --- /dev/null +++ b/program/program/check-valid-date/CheckValidDate.swift @@ -0,0 +1,15 @@ +import Foundation + +func isValidDate(_ dateString: String) -> Bool { + let dateFormatter = DateFormatter() + dateFormatter.dateFormat = "yyyy-MM-dd" + dateFormatter.locale = Locale(identifier: "en_US_POSIX") + dateFormatter.timeZone = TimeZone(secondsFromGMT: 0) + dateFormatter.isLenient = false + return dateFormatter.date(from: dateString) != nil +} + +// Example usage +let inputDate = "2019-02-29" +let result = isValidDate(inputDate) +print("Output: \(result)")