objective c - NSDateFormatter issue involving week numbers -


on machine (regional settings united states), default "short" date format set "1/5/13" (month/day/year).

in system preferences, have appended week number, "1/5/13 1".

my problem code, try convert string date:

nsdateformatter *dateformatter = [[nsdateformatter alloc] init]; [dateformatter setdatestyle:nsdateformattershortstyle]; nsdate *date = [dateformatter datefromstring:@"1/5/13 1"]; nslog(@"date: %@", date); 

on machine, prints:

date: 2000-01-01 05:00:00 +0000 

that's 2000, not close 2013.

what causing problem?

i'm late discussion, , have seen number of solutions offered, see wrong them 2 incompatible date format elements being used together. original approach failed because string you're attempting date doesn't match nsdateformattershortstyle provides for.

if you're going evaluate week of year, you've got use capitalized form of year; provides "week of year" kind of calendar.

let's re-work original code bit include new format:

nsdateformatter *dateformatter = [[nsdateformatter alloc] init]; [dateformatter setdateformat:@"mm/dd/yy w"]; nsdate *date = [dateformatter datefromstring:@"1/5/13 1"]; nslog(@"date: %@", date); 

note i'm using capitalized form year. 1 week of year, this:

date: 2013-01-05 08:00:00 +0000 

and 2 week of year:

date: 2013-01-12 08:00:00 +0000 

and 3:

date: 2013-01-19 08:00:00 +0000 

makes sense, doesn't it? day in date increments 7 days each increment 1 of week of year. time's dorked, of course, never discussed evaluate in area, did we?


Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -