简体   繁体   中英

how to convert Javascript Date to iOS date format

I am new in iOS development. Actually I am trying to show some information which I get from a web service in a table view. I have successfully retrieved the response as JSON in my code, but in my table view there is a label to show date. But in my response the date is something like this

/Date(1391068800000)/

How can I convert this to show in my table view? I think this is Javascript date. But I'm not sure how to convert it.

NSDate *date = [NSDate dateWithTimeIntervalSince1970:[your timestamp doublevalue]/1000];

除以1000的原因毫秒(13位)

The date in your response is a timestamp. You can create a date object by:
NSDate *date = [NSDate dateWithTimeIntervalSince1970:1391068800000];

After that you can convert this date to a string in an appropriate format by using the NSDateFormatter class. For example:

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];  
[formatter setDateStyle:NSDateFormatterMediumStyle];  
NSString *dateString = [formatter stringFromDate:date];

For more information about formatting dates see: https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM