Get milliseconds from time and construct time based on milliseconds

Ferhat Kurtulmuş aferust at gmail.com
Tue May 28 18:29:17 UTC 2024


On Tuesday, 28 May 2024 at 17:37:42 UTC, bauss wrote:
> I have two questions that I can't seem to find a solution to 
> after looking at std.datetime.
>
> First question is how do I get the current time but in 
> milliseconds?
>
> Second is how do I construct a time ex. systime or datetime 
> based on milliseconds?
>
> Thanks

Unixtime might be what you want:

import std;

import std.datetime;
import std.stdio;

     void main() {
         // Get the current time in the UTC time zone
         auto currentTime = Clock.currTime();

         // Convert the time to the Unix epoch 
(1970-01-01T00:00:00Z)
         Duration unixTime = currentTime - SysTime(DateTime(1970, 
1, 1), UTC());

         // Get the total milliseconds
         long milliseconds = unixTime.total!"msecs";

         // Print the Unix time in milliseconds
         writeln("Unix time in milliseconds: ", milliseconds);
     }



More information about the Digitalmars-d-learn mailing list