3

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];
iGatiTech
  • 2,306
  • 1
  • 21
  • 45
Will Smith
  • 339
  • 1
  • 4
  • 13

5 Answers5

4

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);
TheTiger
  • 13,264
  • 3
  • 57
  • 82
  • `NSDateFormatter` will set the format what kind of date you want ... but as he asked this code is okay. He dont want the format he is asking for `NSDate` to `NSString` ... – TheTiger Oct 04 '12 at 11:24
  • NSDateFormatter `stringFromDate` will convert NSDate to NSString. – Hot Licks Oct 04 '12 at 11:26
  • Every one know that but look at the question he asking without `NSDateFormatter` and i this is converting what is the issue .. ??? – TheTiger Oct 04 '12 at 11:28
  • Converting with `description` is not supported for "live" code -- it's only for test/diagnostics. – Hot Licks Oct 04 '12 at 11:29
  • This is @Mahesh Dhapa's problem i just gave the answer according to question... :) In my code i would never do that without `NSDateFormatter` ... According to question this is okay. – TheTiger Oct 04 '12 at 11:31
  • `NSString *stringDate = (NSString *)pickerDate;` does not convert the `pickerDate` in any way. – Martin R Oct 04 '12 at 11:31
  • @MartinR -- Yep, all casting does is hide an NSDate in a variable labeled NSString. When you later use the variable in a `%@` format, `description` will be invoked, so it *seems* like casting was effective, but it does nothing. – Hot Licks Oct 04 '12 at 11:41
  • Note that `[NSString stringWithFormat:@"%@",pickerDate]` does a `[pickerDate description]` under the covers. – Hot Licks Oct 04 '12 at 11:44
  • @VakulSaini - Yes, the formatter is the best way. `description` *should not* be used in "real" code, only diagnostics. – Hot Licks Oct 04 '12 at 11:48
  • Good!! ... Thanks for sharing the knowledge. – TheTiger Oct 04 '12 at 11:50
3

Folks, read the documentation for NSDate, fer cryin' out loud!!!

description

Returns a string representation of the receiver.

- (NSString *)description

Return Value

A string representation of the receiver.

Discussion

The representation is not guaranteed to remain constant across different releases of the operating system. To format a date, you should use a date formatter object instead (see NSDateFormatter and Data Formatting Guide)

It should also be understood that when you do [NSString stringWithFormat:@"%@", something], what occurs is that the code behind %@ formatting invokes the description method of something. For NSString objects the description method is defined to reliably return the string value, but for all other Cocoa classes description is intended for diagnostic use only.

The other alternative to NSDateFormatter (besides the backwards NSCalendar scheme) is to crack the date yourself -- not that difficult, really, and at least an interesting exercise.

[If you want to try cracking the date yourself, use timeIntervalSinceReferenceDate, which returns an NSTimeInterval object (really just a typecast double) that represents seconds since Jan 1 2001 (negative for dates before then). The value will be UTC, so add/subtract any timezone offset if you want local time, then start using modulo arithmetic. You can easily get it to milliseconds, seconds, minutes, and hours, plus a "days since Jan 1 2001" value. From there converting to day/month/year requires thought, but is not terribly difficult, especially if you confine yourself to dates after 1900.]

Hot Licks
  • 47,103
  • 17
  • 93
  • 151
1

I am using this and saving in to database as string only.

NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@"EEE, d MMM yyyy HH:mm:ss z"];

NSString *dateString = [dateFormatter stringFromDate:[NSDate date]];  
NSLog(@"Current time date:%@", dateString);
TheTiger
  • 13,264
  • 3
  • 57
  • 82
iPhone Programmatically
  • 1,211
  • 2
  • 22
  • 54
  • The only "gotcha" is that [you should set the date formatter's locale to "en_US".](http://stackoverflow.com/questions/6613110/what-is-the-best-way-to-deal-with-the-nsdateformater-locale-feature) – Hot Licks Oct 04 '12 at 11:28
0

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

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

NSString *stringDate = [pickerDate description];
NSLog(@"%@", stringDate);
saraman
  • 568
  • 7
  • 19
0
NSDate *date = [NSDate date];
NSDateFormatter *dateFormatter = [NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"EEE, d MMM yyyy HH:mm:ss z"];
NSString *dateString = [dateFormatter stringFromDate:date];
NSLog(@"Current time date:%@", dateString);
garrettmurray
  • 3,338
  • 1
  • 25
  • 23