• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: NSDateFormatter refuses to format dates
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSDateFormatter refuses to format dates


  • Subject: Re: NSDateFormatter refuses to format dates
  • From: Vincent Spader <email@hidden>
  • Date: Tue, 27 Sep 2011 14:51:32 -0700

On Sep 27, 2011, at 1:41 AM, Gerriet M. Denkmann wrote:

> The following code produces an empty string:
>
> 	calender = [ [ NSCalendar autoupdatingCurrentCalendar ] retain ];
> 	NSString *dateTemplate = @"eeeyyyyMMMMddHHmmss";
> 	NSLocale *locale = [calender locale];	//	en_IE
> 	NSString *dateFormat = [ NSDateFormatter dateFormatFromTemplate: dateTemplate  options:0  locale: locale ];	//	EEE dd MMMM yyyy HH:mm:ss
> 	dateFormatter = [ [ NSDateFormatter alloc ] initWithDateFormat: dateFormat allowNaturalLanguage: NO ];
> 	[ dateFormatter setFormatterBehavior: NSDateFormatterBehavior10_4 ];
> 	[ dateFormatter setLocale: locale ];
> 	[ dateFormatter setGeneratesCalendarDates: YES ];
> 	NSTimeZone *localTimeZone = [ calender timeZone ];
> 	[ dateFormatter setTimeZone: localTimeZone ];
> 	NSLog(@" now \"%@\"", [dateFormatter stringFromDate: [NSDate date]]);
>
> What am I missing?
Creating an NSDateFormatter via -initWithDateFormat:allowNaturalLanguage: will create a 10.0 style date formatter. 10.0 and 10.4+ date formatters have completely different format strings, so when you call -setFormatterBehavior:, the format string may not stick around. Here's what you want:
	dateFormatter = [ [ NSDateFormatter alloc ] init];
	[dateFormatter setDateFormat:dateFormat];

and the setFormatterBehavior: call is no longer necessary. You can find more info in the "Formatting Behaviors and OS Versions" section of the NSDateFormatter reference.

--Vince

>
> Kind regards,
>
> Gerriet.
>
> P.S. 10.7.1
>
> _______________________________________________
>
> Cocoa-dev mailing list (email@hidden)
>
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: NSDateFormatter refuses to format dates
      • From: "Gerriet M. Denkmann" <email@hidden>
References: 
 >NSDateFormatter refuses to format dates (From: "Gerriet M. Denkmann" <email@hidden>)

  • Prev by Date: Re: Equivalent of UITextField's textField:shouldChangeCharactersInRange:replacementString for NSTextField
  • Next by Date: Re: debugging crashes in dyld?
  • Previous by thread: Re: NSDateFormatter refuses to format dates
  • Next by thread: Re: NSDateFormatter refuses to format dates
  • Index(es):
    • Date
    • Thread