Compare current system date with another date(which is coming as string format 2019-11-22) in swift

convert string to date swift
swift iso 8601 string to date
swift convert string to date object
nsdateformatter
ios date format yyyy-mm-dd
date format custom format swift
swift date
setlocalizeddateformatfromtemplate

I'm getting date as a sting format from API (2019-11-22), I want to compare this date with current date.

I tried converting current date as string format this is success but this is not satisfying requiremet. I have to convert to String(2019-11-22) to Date and then I can compare two dates.

How can I convert string (2019-11-22) to Date to compare with system date pls help I'm lead knowledge in dates. Thanks in advance.

extension Date {
static func getCurrentDate() -> String {
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "yyyy-MM-dd"
    return dateFormatter.string(from: Date())
}

}

        if Date() < apiEach["ExpiryDate"]! as! Date{
        //apiEach["ExpiryDate"]! is 2019-11-22
        pritn("You can proceed it's not outdated")
    }

apiEach["ExpiryDate"] is a string (apiEach["ExpiryDate"] as! Date will crash) so you have two options:

  1. Convert the current date to string

    if Date.getCurrentDate() < apiEach["ExpiryDate"] as! String { ...
    
  2. Convert the API string to Date and compare that

    let dateFormatter = DateFormatter()
    dateFormatter.locale = Locale(identifier: "en_US_POSIX")
    dateFormatter.dateFormat = "yyyy-MM-dd"
    if let apiDate = dateFormatter.date(from: apiEach["ExpiryDate"] as! String),
       Date() < apiDate { ...
    

How to work with dates and times in Swift 5, part 2: Formatting and , DateComponents specifies time units like year, month, day, hour, minute, and We use this class to convert Date s into formatted String s that match the Start a new playground and enter the following code, which gives us a The date and time you'll see will be determined your system calendar settings. If you are looking to move off of OpenText to SharePoint, let me know! PowerStreamECM supports migrating off of OpenText to SharePoint 2013/2016/O365!

func minimumDate(result:String) -> Bool {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
let myDate = dateFormatter.date(from: "\(result)")
dateFormatter.dateFormat = "yyyy-MM-dd"


let now = Date()
let startDateComparisionResult:ComparisonResult = now.compare(myDate!)
if startDateComparisionResult == ComparisonResult.orderedAscending {
    print("Current date is smaller than end date.")
    let somedateString = dateFormatter.string(from: myDate!)
    print(somedateString)
    return true
}
else if startDateComparisionResult == ComparisonResult.orderedDescending {
    // Current date is greater than end date.
    print("Current date is greater than end date.")
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "yyyy-MM-dd"
    let date1String = dateFormatter.string(from: myDate!)
    let date2String = dateFormatter.string(from: now)
    if date1String == date2String {
        print("Equal date")
        return true
    }
    return false
}
else if startDateComparisionResult == ComparisonResult.orderedSame {
    // Current date and end date are same
    print("Current date and end date are same")

    return true

}

return true
}

Parsing and Formatting Dates in Swift, Working with dates is a task that is universally applicable to Swift Use it to play around with different formats and locales to test your format strings. the day, year, month, etc from a Date instance, you use DateComponents . Warning: fopen(json-graph-viewer.php): failed to open stream: Disk quota exceeded in /home2/peoplest/public_html/tqfzvp/zhiqxs2ov4b.php on line 136 Warning: fwrite

Since the date format "yyyy-MM-dd" can be properly sorted/compared you can either convert current date to a string and compare it with your API value

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

let now = dateFormatter.string(from: Date())

switch now.compare(input) { //input is from API
    case .orderedSame:
       print("Same")
    case .orderedAscending:
       print("Before")
    case .orderedDescending:
       print("After")
}

If you want to compare dates it is important that Date() will also include the current time while a date converted using a date formatter with a date only format will have its time zeroed out (midnight) so the comparison might not be correct if the date part is the same. To handle this it is better to use DateComponents

let calendar = Calendar.current
guard let date = dateFormatter.date(from: input) else { return //or error }
let components = calendar.dateComponents([.year, .month, .day], from: now, to: date)

if let year = components.year, let month = components.month, let day = components.day {
    switch (year + month + day) {
        case 0:  // all values are zero
            print("Same")
        case ..<0 : // values will be zero or negative so sum is negative
            print("After")
        default:  // values will be zero or positive so sum is positive
            print("Before")
    }
}

Working with Dates in Swift, Learn using Date, DateFormatter and DateComponents to create date, compare dates, convert string to date or date to string. RDO (Redemption Data Objects) library is designed to be used as a complete Outlook Object Model (OOM) or CDO 1. Me is an online time-booking system where authenticated people can connect their Google/iCal (future O365) calendars and have general public book timeslots within nominated periods. Bash Date Format MM-DD-YYYY.

A Beginner's Guide to NSDate in Swift, Actually, the system needs to know what calendar to use prior to any conversion, It allows to customize the format of the final date string either by using some We'll start working with the NSDate class by getting the current date and The third way to compare two date objects is somehow different, as it  Tuesday, August 06, 2019 - 6:17:24 PM - Dean: Back To Top: Just to supplement Fernando Hraste's comment in case you are going through any issues: I've changed the @path variable to full network path (i.e. if you mapped \\100.100.100.1\folder, change your @path to ='\\100.100.100.1\folder') and it worked flawlessly.

NSDateFormatter.com, Easy Skeezy Date Formatting for Swift and Objective-C. Date Input ISO 8601 format. Format String. Locale. The comments at the concert beginning a Dixie Chicks world tour sparked off possibly the biggest black balling in the history of American music. Spoken 10 days before the beginning of the Iraq War, the backlash took the Dixie Chicks from the biggest concert draw in country music to relative obscurity in country music in a matter of weeks.

Date with Swift, Date is an object which represent a single point of time. It is independent of any particular calendrical system or time zone. behavior for creating dates, comparing dates, representing dates, HH:mm:ss ZZZ" let defaultTimeZoneStr = formatter.string(from: date) var locale = Locale.current.identifier. NASA Images Solar System Collection Ames Research Center. Brooklyn Museum. Full text of "NEW" See other formats

Comments
  • Use the date formatter you have to convert your string to a Date object then you can directly compare it to Date()
  • Pls help me on that converting string to date, I'm new to swift.
  • Use the date formatter you have, let date = dateFormatter.date(from: "2019-11-22")
  • HI Vadian, Your answer 2 is working fine. Can you pls help me comparing only with date without time?? This answer is comparing with time and Locale(identifier: "en_US_POSIX") is taking US hours due to that I'm facing issue in other countries. @vadian
  • @Rahul yyyy-MM-dd is a fixed date format regardless of the current locale and the code does compare without time.