Cocoa Nub
Cocoa Nub

Reputation: 499

NSDateFormatter returning Null

I have this method on an object:

  - (NSString *)formattedScheduledOn
  {
      NSDateFormatter *dateFormater = [[NSDateFormatter alloc] init];
      [dateFormater setDateFormat:@"MM/dd/yyyy hh:mm"];
      return [dateFormater stringFromDate:self.scheduledOn];
  }

scheduledOn is an NSDate object. Showing it as a raw date yields something like: 2013-03-22T09:44:28-07:00

But I always get null from that method. Any ideas? Thanks!

Upvotes: 0

Views: 315

Answers (2)

nsgulliver
nsgulliver

Reputation: 12671

Make sure your date is not nil. Try the following code and see what you get in return

  NSDateFormatter *dateFormater = [[NSDateFormatter alloc] init];
  [dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
  [dateFormater setDateFormat:@"MM/dd/yyyy hh:mm"];
  return [dateFormater stringFromDate:[NSDate date]];

If change you passing the date as string then you should set the format according to the date string passed.

[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssz"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];

Upvotes: 2

iDeviceGuy
iDeviceGuy

Reputation: 993

Why not do something like this?

Set your datePicker to pick or whatever you want.

[pick addTarget:self action:@selector(updateDateLabel:) forControlEvents:UIControlEventValueChanged];

-(IBAction)updateDateLabel:(id)sender {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterLongStyle];
    [formatter setTimeStyle:NSDateFormatterMediumStyle];
    dateLabel.text = [formatter stringFromDate:pick.date];
}

Upvotes: -1

Related Questions