Skip to content
This repository has been archived by the owner on Jul 10, 2024. It is now read-only.

Commit

Permalink
Create CheckValidDate.swift (#5618)
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
bulutg and RiteshK-611 committed Apr 11, 2024
1 parent 63a86ff commit a6713d3
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions program/program/check-valid-date/CheckValidDate.swift
Original file line number Diff line number Diff line change
@@ -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)")

0 comments on commit a6713d3

Please sign in to comment.