Can any one give me an idea how to get the current date in milliseconds?
-
possible duplicate of [How can I get a precise time, for example in milliseconds in objective-c?](http://stackoverflow.com/questions/889380/how-can-i-get-a-precise-time-for-example-in-milliseconds-in-objective-c) – jscs May 28 '11 at 06:06
-
When you say the current date in milliseconds, what do you mean? Can you provide an example of your expected output for a specific date? – lnafziger Jun 22 '12 at 15:41
12 Answers
There are several ways of doing this, although my personal favorite is:
CFAbsoluteTime timeInSeconds = CFAbsoluteTimeGetCurrent();
You can read more about this method here. You can also create a NSDate object and get time by calling timeIntervalSince1970 which returns the seconds since 1/1/1970:
NSTimeInterval timeInSeconds = [[NSDate date] timeIntervalSince1970];
And in Swift:
let timeInSeconds: TimeInterval = Date().timeIntervalSince1970

- 4,699
- 1
- 26
- 26
-
4im trying tis following code NSTimeInterval milisecondedDate = ([[NSDate date] timeIntervalSince1970] * 1000); NSLog(@"didReceiveResponse ---- %d",milisecondedDate); – siva May 27 '11 at 10:23
-
-
2The problem is with the NSLog statement. NSTimeInterval is a typedefed double. Thus you should use %f instead of %d. – Pawel May 28 '13 at 13:58
-
59[[NSDate date] timeIntervalSince1970] returns an NSTimeInterval, which is a duration in seconds, not milli-seconds. – Erik van der Neut Sep 10 '14 at 04:20
-
12Note that `CFAbsoluteTimeGetCurrent()` returns the time relative to the reference date `Jan 1 2001 00:00:00 GMT`. Not that a reference date was given in the question, but be aware that this is not a UNIX timestamp. – nyi Sep 17 '14 at 14:57
-
1
-
2[[NSDate date] timeIntervalSince1970] returns time in seconds and not in milliseconds. – SAPLogix Jan 31 '15 at 11:37
-
@VaibhavSaran The `timeIntervalSince1970` is since January 1, 1970 UTC (in seconds). And UTC is identical to GMT. – thedp May 12 '15 at 19:59
-
but isn't CFAbsoluteTimeGetCurrent() return time in "seconds" unit also? – Hlung Mar 15 '17 at 08:36
-
Casting the NSTimeInterval directly to a long overflowed for me, so instead I had to cast to a long long.
long long milliseconds = (long long)([[NSDate date] timeIntervalSince1970] * 1000.0);
The result is a 13 digit timestamp as in Unix.

- 1,219
- 12
- 9
-
4This wont return the actual milliseconds because timeIntervalSince1970 returns the interval in seconds, so we wont have the desired millis accuracy. – Danpe Aug 18 '15 at 16:47
-
9The `timeIntervalSince1970 ` method does return at the seconds on the whole number, however it is a `double` that also includes the fractional second as well which can be arithmetically converted to milliseconds. Hence, the multiplication by 1000. – wileymab Aug 24 '15 at 20:57
-
-
2
-
NSTimeInterval milisecondedDate = ([[NSDate date] timeIntervalSince1970] * 1000);

- 48,927
- 17
- 132
- 168
-
im trying tis following code NSTimeInterval milisecondedDate = ([[NSDate date] timeIntervalSince1970] * 1000); NSLog(@"didReceiveResponse ---- %d",milisecondedDate); -- but it showing the value in negative val -- like ResponseTIME ---- 556610175 ResponseTIME ---- -1548754395 – siva May 27 '11 at 10:25
-
1try using %f placeholder instead of %d. If that doesn't help - remove the multiplication by 1000 – Eimantas May 27 '11 at 10:30
-
@Eimantas,When i try to use %f ... Im getting time as Follows Response Time = 1306494959011.239014 Response Time = 1306494910724.744141 If it's ms then the above time is more than an hour. – siva May 27 '11 at 12:30
-
The current date is time interval in seconds since 1970. You didn't mention about reference date anything. – Eimantas May 27 '11 at 12:55
-
-
-
i mean,i want to display the current date in the form of millisecond,Pls get the proper solutions – siva May 30 '11 at 05:25
-
`NSTimeInterval`, though it's just a typedef for `double`, is used only to express seconds (and fractions of seconds). Semantically, it makes no sense to assign to a value representing milliseconds to an `NSTimeInterval` variable. – kpozin Apr 24 '13 at 13:47
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

- 108
- 1
- 5
extension NSDate {
func toMillis() -> NSNumber {
return NSNumber(longLong:Int64(timeIntervalSince1970 * 1000))
}
static func fromMillis(millis: NSNumber?) -> NSDate? {
return millis.map() { number in NSDate(timeIntervalSince1970: Double(number) / 1000)}
}
static func currentTimeInMillis() -> NSNumber {
return NSDate().toMillis()
}
}

- 941
- 12
- 19
@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];

- 8,540
- 5
- 45
- 65
-
NSString *timestampString = [NSString stringWithFormat:@"%ld%03d",digits ,decimalDigits]; is correct actually. In your case if decimalDigits value is less than 100 will produce wrong result. – PANKAJ VERMA Jan 11 '18 at 11:14
As mentioned before, [[NSDate date] timeIntervalSince1970] returns an NSTimeInterval, which is a duration in seconds, not milli-seconds.
You can visit https://currentmillis.com/ to see how you can get in the language you desire. Here is the list -
ActionScript (new Date()).time
C++ std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count()
C#.NET DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
Clojure (System/currentTimeMillis)
Excel / Google Sheets* = (NOW() - CELL_WITH_TIMEZONE_OFFSET_IN_HOURS/24 - DATE(1970,1,1)) * 86400000
Go / Golang time.Now().UnixNano() / 1000000
Hive* unix_timestamp() * 1000
Java / Groovy / Kotlin System.currentTimeMillis()
Javascript new Date().getTime()
MySQL* UNIX_TIMESTAMP() * 1000
Objective-C (long long)([[NSDate date] timeIntervalSince1970] * 1000.0)
OCaml (1000.0 *. Unix.gettimeofday ())
Oracle PL/SQL* SELECT (SYSDATE - TO_DATE('01-01-1970 00:00:00', 'DD-MM-YYYY HH24:MI:SS')) * 24 * 60 * 60 * 1000 FROM DUAL
Perl use Time::HiRes qw(gettimeofday); print gettimeofday;
PHP round(microtime(true) * 1000)
PostgreSQL extract(epoch FROM now()) * 1000
Python int(round(time.time() * 1000))
Qt QDateTime::currentMSecsSinceEpoch()
R* as.numeric(Sys.time()) * 1000
Ruby (Time.now.to_f * 1000).floor
Scala val timestamp: Long = System.currentTimeMillis
SQL Server DATEDIFF(ms, '1970-01-01 00:00:00', GETUTCDATE())
SQLite* STRFTIME('%s', 'now') * 1000
Swift* let currentTime = NSDate().timeIntervalSince1970 * 1000
VBScript / ASP offsetInMillis = 60000 * GetTimeZoneOffset()
WScript.Echo DateDiff("s", "01/01/1970 00:00:00", Now()) * 1000 - offsetInMillis + Timer * 1000 mod 1000
For objective C I did something like below to print it -
long long mills = (long long)([[NSDate date] timeIntervalSince1970] * 1000.0);
NSLog(@"Current date %lld", mills);
Hopw this helps.

- 66,731
- 38
- 279
- 289
Cconvert NSTimeInterval
milisecondedDate
value to nsstring
and after that convert into int
.

- 19
- 1
You can use following methods to get current date in milliseconds.
[[NSDate date] timeIntervalSince1970];
OR
double CurrentTime = CACurrentMediaTime();

- 1
- 1

- 19,140
- 8
- 51
- 70
- (void)GetCurrentTimeStamp
{
NSDateFormatter *objDateformat = [[NSDateFormatter alloc] init];
[objDateformat setDateFormat:@"yyyy-MM-dd"];
NSString *strTime = [objDateformat stringFromDate:[NSDate date]];
NSString *strUTCTime = [self GetUTCDateTimeFromLocalTime:strTime];//You can pass your date but be carefull about your date format of NSDateFormatter.
NSDate *objUTCDate = [objDateformat dateFromString:strUTCTime];
long long milliseconds = (long long)([objUTCDate timeIntervalSince1970] * 1000.0);
NSString *strTimeStamp = [NSString stringWithFormat:@"%lld",milliseconds];
NSLog(@"The Timestamp is = %@",strTimeStamp);
}
- (NSString *) GetUTCDateTimeFromLocalTime:(NSString *)IN_strLocalTime
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
NSDate *objDate = [dateFormatter dateFromString:IN_strLocalTime];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
NSString *strDateTime = [dateFormatter stringFromDate:objDate];
return strDateTime;
}
Use this to get the time in milliseconds (long)(NSTimeInterval)([[NSDate date] timeIntervalSince1970])
.

- 9,147
- 4
- 43
- 63

- 917
- 2
- 7
- 19
An extension on date is probably the best way to about it.
extension NSDate {
func msFromEpoch() -> Double {
return self.timeIntervalSince1970 * 1000
}
}

- 7,845
- 6
- 49
- 72