开发者

Use (Billy Meltdown) NSDate-helper methods to convert Unix timestamp and display?

开发者 https://www.devze.com 2023-04-12 01:19 出处:网络
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.

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];
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号