Pavle Jonoski
By Pavle Jonoski

Get current time in milliseconds in all popular programing languages

Get current time in milliseconds in all popular programing languages

Get the current time as milliseconds in:

Python

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import datetime



# Microseconds precision, returned as float number

curr_time_millis = datetime.datetime.now().timestamp()*1000



print(curr_time_millis) 

>> 1590571047186.401

#  [ millis    ].[ microseconds]



# Get just the milliseconds

curr_time_millis = int(datetime.datetime.now().timestamp()*1000)



print(curr_time_millis) 

>> 1590571047186

JavaScript

1
2
3
4
5
6
7
8
9
10
11
12
13


currTimeMillis = new Date().getTime() // returns unix timestamp in milliseconds



console.log(currTimeMillis)

>> 1590571047186



Java

1
2
3
4
5
6
7
8
9
10
11


long currTimeMillis = System.getCurrentTimeMillis();



System.out.println(currTimeMillis);

>> 1590571047186

Swift

1
2
3
4
5
6
7
var currTimeMillis = Int64(NSDate().timeIntervalSince1970 * 1000)



>> 1590571047186

Swift 3.0

1
2
3
4
5
6
7
var currTimeMillis = Int64(Date().timeIntervalSince1970 * 1000)



>> 1590571047186

Go

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27


import (

    "fmt"

    "time"

)



func main() {

    currTimeMillis := time.Now().UnixNano() / int64(time.Millisecond)

    fmt.Printf("%d\n", currTimeMillis)

}



>> 1590571047186



C#

1
2
3
long currTimeMillis = DateTimeOffset.Now.ToUnixTimeMilliseconds();

C++

With C++ 11:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19


#inclide <chrono>



using namespace std::chrono;



unsigned long long curr_time_millis = duration_cast<milliseconds>(

    system_clock::now().time_since_epoch()

).count();



With time.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25


#include <sys/time.h>





struct timeval tv;



gettimeofday(&tv, NULL);



unsigned long long curr_time_millis = 

    (unsigned long long) tv.time_sec * 1000 +  // time_sec is the number of seconds

    (unsigned long long) tv.time_usec / 1000;  // time_usec is the number of microseconds



Scala

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17


// same as in Java



val currTimeMillis = System.getCurrentTimeMillis();



// alternatively



val currTimeMillis = DateTime.now(DateTimeZone.UTC).getMillis()

Kotlin

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// Same as in Java

var currTimeMillis = System.currentTimeMillis()



// alternatively, using kotlin.system.getTimeMillis



import kotlin.system.getTimeMillis



var currTimeMillis = getTimeMillis()

Ruby

1
2
3
(Time.now.to_f * 1000).to_i

PHP

1
2
3
$curr_time_millis = round(microtime(true) * 1000);

C

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25


#include <sys/time.h>





struct timeval tv;



gettimeofday(&tv, NULL);



unsigned long long curr_time_millis = 

    (unsigned long long) tv.time_sec * 1000 +  // time_sec is the number of seconds

    (unsigned long long) tv.time_usec / 1000;  // time_usec is the number of microseconds



Rust

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15


use std::time::{SystemTime, UNIX_EPOCH};



fn main() {

  let curr_time_millis = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).expect("We're before UNIX epoch.") * 1000;

  println!("{:?}", curr_time_millis);

}

Objective-C


NSDate *date = [NSDate date];



double curr_time_millis = [date timeIntervalSince1970] * 1000.0;

Dart

1
2
3
var currTimeMillis = DateTime.now().millisecondsSinceEpoch;

Lua

Use the “socket” package

1
2
3
4
5
require "socket"

currTimeMillis = socket.gettime()*1000

Haskell

1
2
3
4
5
6
7
8
9
10
11
import Data.Time.Clock.POSIX (getPOSIXTime)





main = do

  currTimeMillis <- round . (1000 *) <$> getPOSIXTime

Clojure

1
2
3

(System/currentTimeMillis)

Matlab/Octave


currTimeMillis = round(time() * 1000)

R

1
2
3

currTimeMillis = as.numeric(Sys.time())*1000