DatePredicate

public struct DatePredicate : Predicate

The DatePredicate struct is used to evaluate that a given input has a valid date format.

let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "YYYY-MM-dd"

let predicate = DatePredicate(formatter: dateFormatter)
let isValidDate = predicate.evaluate(with: "2021-12-06")
  • Declaration

    Swift

    public typealias InputType = String
  • Returns a new DatePredicate instance.

    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "YYYY-MM-dd"
    
    let predicate = DatePredicate(formatter: dateFormatter)
    let isValidDate = predicate.evaluate(with: "2021-12-06")
    

    Declaration

    Swift

    public init(formatter: DateFormatter)

    Parameters

    formatter

    The DateFormatter instance used to parse the input.

  • Returns a Boolean value that indicates whether a given input has a valid date format or not.

    Declaration

    Swift

    public func evaluate(with input: String) -> Bool

    Parameters

    input

    The input against which to evaluate the receiver.

    Return Value

    true if input has a valid date format, otherwise false.