Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get NSDate day, month and year in integer format?

I want to get the day, month and year components of NSDate in integer form i.e. if the date is 1/2/1988 then I should get 1, 2 and 1988 separately as an integer. How can I do this in iOS? I found the similar question but the method descriptionWithCalendarFormat: gives a warning and seems to be deprecated by now.

like image 504
Yogi Avatar asked Jun 02 '11 11:06

Yogi


People also ask

How do you declare a date variable in Swift?

Creating a Date and Time in Swift Of course, it would be easier to use things like years, months, days and hours (rather than relative seconds) to make a Date . For this you can use DateComponents to specify the components and then Calendar to create the date. The Calendar gives the Date context.


1 Answers

Here you are,

NSDate *currentDate = [NSDate date]; NSCalendar* calendar = [NSCalendar currentCalendar]; NSDateComponents* components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:currentDate]; // Get necessary date components   [components month]; //gives you month  [components day]; //gives you day  [components year]; // gives you year 

You can use NSDateComponents for that as above.

Please visit this page for details.

Hope it helps.

like image 118
Janak Nirmal Avatar answered Oct 16 '22 17:10

Janak Nirmal