Retrieve current date without leading zeroes

2.4k Views Asked by At

I am trying to compare the date chosen on a calendar (Kal calendar implementation) with the current date. The issue is that the current date is in MM/dd/yyyy format, whereas the Kal dates will have no leading zeroes if the month or day is below 10. Is there an easy way to retrieve the current date so that it will not have leading zeroes for the day or month if it's under 10? (The current date will actually be utilized as an attribute for saved objects, so that they can later be queried using the date selected with Kal)

example current date - 07/07/2014

example Kal date - 7/7/2014

2

There are 2 best solutions below

0
jscs On BEST ANSWER

Don't compare strings holding dates. Create actual NSDate objects, using an NSDateFormatter, and compare those. The format strings you need are "MM/dd/yyyy" for your date, however you're retrieving it, and "M/d/yyyy" for Kal's date.

0
Jack On

I have used en_US_POSIX along with "MM/dd/yyyy" & it's Working Fine

let formatter = DateFormatter()
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.dateFormat = "MM/dd/yyyy"

https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html#//apple_ref/doc/uid/TP40002369-SW7