Tuesday, 15 June 2010

ios - Formatting NSDate not working -



ios - Formatting NSDate not working -

i have next method, want utilize homecoming modified date:

- (nsdate *)getcreationdate:(nsfilemanager *)filemanager atpath:(nsstring *)path { nserror *error; nsdate *date; nsdictionary *fileattributes = [filemanager attributesofitematpath:path error:&error]; // creation date. if (!error) { if (fileattributes != nil) { nsdate *creationdate = [fileattributes filecreationdate]; nsstring *datestring = [creationdate description]; nslog(@"unformatted date created: %@", datestring); // format date. nsdateformatter *dateformatter = [[nsdateformatter alloc] init]; [dateformatter setdateformat:@"dd-mm-yyyy hh:mm:ss"]; date = [[nsdate alloc] init]; date = [dateformatter datefromstring:datestring]; nslog(@"formatted date created: %@", [date description]); } else { nslog(@"file attributes not found."); } } else { nslog(@"%@", [error localizeddescription]); } homecoming date; }

the problem formatted date coming null.

output:

unformatted date created: 2013-02-06 04:44:57 +0000

formatted date created: (null)

there's no such thing formatted nsdate. it's date no format. description method debugging , logging , uses whatever format wants.

nsdateformatter used create nsstring representation of nsdate specified format. method replaced , same thing.

- (nsdate *)getcreationdate:(nsfilemanager *)filemanager atpath:(nsstring *)path { nserror *error; nsdictionary *fileattributes = [filemanager attributesofitematpath:path error:&error]; // creation date. if (!error) { if (fileattributes != nil) { homecoming [fileattributes filecreationdate]; } else { nslog(@"file attributes not found."); } } else { nslog(@"%@", [error localizeddescription]); } homecoming nil; }

when want display date, format then. utilize nsdateformatter turn formatted nsstring.

in addition, lines

date = [[nsdate alloc] init]; date = [dateformatter datefromstring:datestring];

create new date, throw away. first line unnecessary.

ios nsdate nsdateformatter

No comments:

Post a Comment