2011年7月20日 星期三

Date Formatter Example

In case you are saving a date in MySQL's "datetime" type, you can parse it as a string and store it as a NSDate object like this:


NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
[formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:28800]];
NSString * date_string = [NSString stringWithFormat:@"%@", [yourdata objectForKey:@"a_date"]];
NSDate * ed = [formatter dateFromString: date_string];
...
[formatter release];

That is! It is really tedious if you have to look back the reference for this convention! This can be a useful remainder.


沒有留言:

張貼留言