Use (Billy Meltdown) NSDate-helper methods to convert Unix timestamp and display?
I would like to convert a Unix timestamp e.g 1315401371 stored as a NSString into a date format of 23:00 12 October 2011.
There are a number of similar questions being ask in relation to this however I wish to use billymeltdown / nsd开发者_JAVA技巧ate-helper methods
I have imported the 2 files (.h and .m) into my project, but have not manage to get further than that. There is documentation, which I have read, however I am still very new to objective C and do not understand how I actual use the libraries.
Thanks in Advance.
You should use NSDateFormatter
NSTimeInterval timestamp = 1315401371;
NSDate *date = [NSDate dateWithTimeIntervalSince1970:timestamp];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"HH:mm dd MMMM yyyy"]; // use one d for 7 October instead of 07 October
// For english month names
NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
[dateFormatter setLocale:usLocale];
[usLocale release];
NSString *dateString = [dateFormatter stringFromDate:date];
[dateFormatter release];
精彩评论