Get current date in milliseconds

前端 未结 12 782
别跟我提以往
别跟我提以往 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:23

    @JavaZava your solution is good, but if you want to have a 13 digit long value to be consistent with the time stamp formatting in Java or JavaScript (and other languages) use this method:

    NSTimeInterval time = ([[NSDate date] timeIntervalSince1970]); // returned as a double
    long digits = (long)time; // this is the first 10 digits
    int decimalDigits = (int)(fmod(time, 1) * 1000); // this will get the 3 missing digits
    long timestamp = (digits * 1000) + decimalDigits;
    

    or (if you need a string):

    NSString *timestampString = [NSString stringWithFormat:@"%ld%d",digits ,decimalDigits];
    

提交回复
热议问题