Will Smith Will Smith - 1 year ago 85
Objective-C Question

Convert Date into String without Using NSDateFormatter

How to convert

without use of

For Your reference my code as follows , i am using it for my reminder application.

Thank You So Much.

NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];

// Get the current date
NSDate *pickerDate = [self->dp date];

// Break the date up into components
NSDateComponents *dateComponents = [calendar components:( NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit )
NSDateComponents *timeComponents = [calendar components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit )

// Set up the fire time
NSDateComponents *dateComps = [[NSDateComponents alloc] init];
[dateComps setDay:[dateComponents day]];
[dateComps setMonth:[dateComponents month]];
[dateComps setYear:[dateComponents year]];
[dateComps setHour:[timeComponents hour]];
// Notification will fire in one minute
[dateComps setMinute:[timeComponents minute]];
[dateComps setSecond:[timeComponents second]];
NSDate *itemDate = [calendar dateFromComponents:dateComps];
[dateComps release];

Answer Source

According to your question you can get it in this way -

NSString *stringDate = [NSString stringWithFormat:@"%@",pickerDate];

NSString *stringDate = [pickerDate description];
NSLog(@"%@", stringDate);

You can also get NSString by using NSDateFormatter why're you getting fear to use it ?

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd/MM/yyyy"];

NSString *stringDate = [dateFormatter stringFromDate:pickerDate];  
NSLog(@"%@", stringDate);