-1

The following is the code I am using to change the date from one format to another. In the following code child.dob is in NSDate as yyyy-MM-dd format I want to change to MMM dd, yyyy format I am able to change the date format however I am getting a warning message here . Please help me to convert date without warning.. Thanks in advance.

  NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSDate *date = [dateFormatter dateFromString:child.dob]; 
    [dateFormatter setDateFormat:@"MMM dd, yyyy"];
    dobLabel.text = [dateFormatter stringFromDate:date];

This is the warning message I am getting: :801: warning: incompatible Objective-C types 'struct NSDate *', expected 'struct NSString *' when passing argument 1 of 'dateFromString:

Dilip Rajkumar
  • 7,006
  • 6
  • 60
  • 76
  • What is the warning you're getting? – Eimantas Oct 04 '11 at 07:13
  • `NSDate` do not know the format they are displayed, they just hold a date object with a timezone. You can use `NSDateFormatter` to create a `NSString` form a `NSDate` object. If you **`NSLog`** a `NSDate` object it will always display the full date with timezone. – rckoenes Oct 04 '11 at 07:18
  • :801: warning: incompatible Objective-C types 'struct NSDate *', expected 'struct NSString *' when passing argument 1 of 'dateFromString:' from distinct Objective-C type this is the warning I am getting – Dilip Rajkumar Oct 04 '11 at 08:38
  • 1
    The warning is becaue in this line: `NSDate *date = [dateFormatter dateFromString:child.dob]; ` You are passing an NSDate, when it expects a NSString. – Tudor Oct 04 '11 at 08:47
  • Thank you for your reply. I agree with you Tudorizer. I need to know how to get rid of this date conversion. – Dilip Rajkumar Oct 06 '11 at 04:55

1 Answers1

0

Yep, you use NSDateFormatter dateFromString to convert an NSDate object to a displayable string. Use NSDateFormatter stringFromDate to go the other way.

If you use NSLog on an NSDate then under the covers it does a description call which presents a fixed format. However, description should not be used for "real" work since the format presented is not rigorously defined and could change at any time.

Note that there is a "feature" of NSDateFormatter in that it can erroneously (in my mind) add or remove an AM/PM value based on phone settings, even when you have explicitly specified the format to NSDateFormatter.

Community
  • 1
  • 1
Hot Licks
  • 47,103
  • 17
  • 93
  • 151
  • Thank you for your reply. This is what I need Daniel one date format to another I tried like this: NSDate *date = child.dob; [dateFormatter setDateFormat:@"MMM dd, yyyy"]; still I am getting error can you guide me to change date from one format to another. – Dilip Rajkumar Oct 07 '11 at 12:41
  • In your original post, you never use the second date format. The date isn't going to format itself. – Hot Licks Oct 07 '11 at 12:59
  • Sorry Daniel, I forgot to included one more line I have edited my code please help. – Dilip Rajkumar Oct 07 '11 at 14:37
  • @DilipRajkumar -- Should work, if child.dob is an NSString in the yyyy-MM-dd format. Of course, you're missing the release of your date formatter, but that just causes a leak -- it won't cause the code to fail. But you continue to fail to say what's not working -- you haven't posted any error messages, etc. You're going to get dinged if you don't start providing better information. – Hot Licks Oct 07 '11 at 16:59
  • In my post I told that child.dob is a NSDate. its in the format yyyy-MM-dd I need the same date as MMM dd, yyyy format and I am able to get it by above code. However that is not the correct way because I am trying to convert a NSDate to NSDate using stringFromDate so I need to know the right procedure. If I try dateFromString I am getting null..The following is the error message i am getting :801: warning: incompatible Objective-C types 'struct NSDate *', expected 'struct NSString *' when passing argument 1 of 'dateFromString: – Dilip Rajkumar Oct 07 '11 at 19:22
  • Then YOY do you use dateFromString to convert an NSDate to an NSDate? Is the name of the function a clue, maybe? (BTW, NSDate objects are not in any "format" -- they are numerical values. If you want a particular format you use stringFromDate.) – Hot Licks Oct 07 '11 at 19:25
  • An NSDate isn't "in" any given format; strictly speaking, it is just a wrapper around an offset from from fixed date. – Paul Lynch Oct 07 '11 at 19:28