• 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
Odd behavior of [NSDateFormatter dateFromString] with short years?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Odd behavior of [NSDateFormatter dateFromString] with short years?


  • Subject: Odd behavior of [NSDateFormatter dateFromString] with short years?
  • From: marc hoffman <email@hidden>
  • Date: Tue, 3 Nov 2009 16:06:46 +0100

Hi,

im running into some very odd behavior when using a ShortStyle date formatter for entering dates, and using single vs double digit year numbers. typing the year as either "2009" or "09" works as expected, but typing it as "9" not only does *not adjust the century, but also produces what i'd consider an invalid date. see the following code and output:

----
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setTimeStyle:NSDateFormatterNoStyle];
    [dateFormatter setDateStyle:NSDateFormatterShortStyle];

    NSDate *date1 = [dateFormatter dateFromString:@"1/1/9"];
    NSDate *date2 = [dateFormatter dateFromString:@"1/1/09"];

NSLog(@"Date1:1/1/9: %@", date1);
NSLog(@"Date2:1/1/09: %@", date2);
----
2009-11-03 16:20:19.886 NSDateFormatterTest[33780:a0f] Date1:1/1/9: 0009-01-01 00:00:00 +020204
2009-11-03 16:20:19.888 NSDateFormatterTest[33780:a0f] Date2:1/1/09: 2009-01-01 00:00:00 +0200


note how date1 not only has the year as "0009", but a timezone of +020204.

this is made worse by the fact that the date *looks* ok (and indistinguishable from the "good" one when displayed thru the NSDataFormatter (say in an NSTableView cell)

is this behavior expected? am i doing something wrong? or is this a bug? can anyone else reproduce this?

thanx,
marc
_______________________________________________

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: Odd behavior of [NSDateFormatter dateFromString] with short years?
      • From: "Hank Heijink (Mailinglists)" <email@hidden>
  • Prev by Date: Re: Handling calculations with many decimal places
  • Next by Date: Re: Odd behavior of [NSDateFormatter dateFromString] with short years?
  • Previous by thread: Re: Handling calculations with many decimal places
  • Next by thread: Re: Odd behavior of [NSDateFormatter dateFromString] with short years?
  • Index(es):
    • Date
    • Thread