Will Smith Will Smith - 5 months ago 18
Objective-C Question

Convert Date into String without Using NSDateFormatter

How to convert

NSDate
into
NSString
without use of
NSDateFormatter
?

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 )
fromDate:pickerDate];
NSDateComponents *timeComponents = [calendar components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit )
fromDate:pickerDate];

// 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

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

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

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);