How to convert NSDate to milliseconds in Objective-C? [duplicate]

。_饼干妹妹 提交于 2019-12-05 18:16:13

问题


For example: assume NSDate tempdate = "05-23-2013". I want this value to be converted into milliseconds.

How can I do this with Objective-C?


回答1:


There is a similar post here and here.

Basically you have get the second form the reference date (1 January 2001, GMT) and multiply it with 1000.

NSTimeInterval seconds = [NSDate timeIntervalSinceReferenceDate];
double milliseconds = seconds*1000;

Cheers!




回答2:


The timeIntervalSince1970 will return seconds from 1970. There are other timeIntervalSince methods if needed.

https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSDate_Class/Reference/Reference.html#//apple_ref/occ/instm/NSDate/timeIntervalSince1970

For reverse conversion, I mean to say from milliseconds to NSDate, refer this stackoverflow article. Convert milliseconds to NSDate

Hope it ll solve your issue.



来源:https://stackoverflow.com/questions/15827657/how-to-convert-nsdate-to-milliseconds-in-objective-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!