How To get Current Time,Date,Month and Year in iPhone Development

Here we go to get current date,month,year and hour using NSCalendar .Apple provides us NSCalendar class to perform this action.

So here we are going to get current day,Year,Month.

1.Get Current date using NSDate:

NSDate *ToDay = [NSDate date]; /// it gives current date with time components.

2.Create NSCalendar Object;

NSCalendar  *Calendar =[ [NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

//we created calendar , it contains components like hour,year,month,day. then lets extract these components from Calendar using NSDateComponents.

3.Step 3:

NSDateComponents *Components = [Calendar components:(NSDayCalendarUnit|NSYearCalendarUnit|NSMonthCalendarUnit|NSHourCalendarUnit) fromDate:ToDay];



4.Step4:

We did it,now lets log it using NSLog;

you have to remember that all these values return NSInteger type values

NSLog(@”Year %i”,[Components year]);
NSLog(@”Month%i”,[Components month]);
NSLog(@”Day%i”,[Components day]);
NSLog(@”Hour%i”,[Components hour]);
.

i hope that this might be useful to you , and thanks in advance for your feedbacks.

Comments

comments

Leave a Reply

Your email address will not be published. Required fields are marked *