How to get current time in milliseconds in Haxe?

3.2k views Asked by At

I need a function that returns the local time in milliseconds on the CPP target.

I tried Haxe's Date class, but Date.now() gives me the time in seconds.

3

There are 3 answers

4
samcodes On BEST ANSWER

Sys.time() * 1000.0 - http://api.haxe.org/Sys.html#time

Gives the most precise timestamp value (in seconds)

To be clear, I tried this and got millisecond resolution on the cpp target. Sys is available on cpp, cs, java, macro, neko, php and python.

2
Andrew On

You could try Date.now().getTime(), however:

Returns the timestamp of the date. It might only have a per-second precision depending on the platforms.

0
Creative Magic On

A fast way of getting a timestamp would be to use the haxe.Timer.stamp() method.

Example:

import haxe.Timer;

var timestamp:Float = Timer.stamp(); // return a timestamp in seconds with fractions

Note that the value itself might differ depending on platforms, only differences between two values make sense.