简体   繁体   中英

Create NSDate with String time from 1970

I have a string that its value is time. The time is formmated with "time from 1970". How can I convert this string to a nsdate object?

I did not understand what you meant by "The time is formatted with 'time from 1970'" But here is how to convert a string formatted long containing the number of seconds since 1970 to a NSDate object:

NSDate * date = [NSDate dateWithTimeIntervalSince1970:[longValueString longLongValue]];

Sometimes the time coming from the servers are in milliseconds. In that case you need to divide it with 1000.

NSDate * date = [NSDate dateWithTimeIntervalSince1970:[longValueString longLongValue]/1000];

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