Get current date in milliseconds

前端 未结 12 771
别跟我提以往
别跟我提以往 2020-12-04 10:23

Can any one give me an idea how to get the current date in milliseconds?

12条回答
  •  时光取名叫无心
    2020-12-04 11:16

    You can just do this:

    long currentTime = (long)(NSTimeInterval)([[NSDate date] timeIntervalSince1970]);
    

    this will return a value en milliseconds, so if you multiply the resulting value by 1000 (as suggested my Eimantas) you'll overflow the long type and it'll result in a negative value.

    For example, if I run that code right now, it'll result in

    currentTime = 1357234941
    

    and

    currentTime /seconds / minutes / hours / days = years
    1357234941 / 60 / 60 / 24 / 365 = 43.037637652207
    

提交回复
热议问题