Getting the current datetime converted into a string for writing to stdout

Gary Willoughby dev at nomad.so
Sat Mar 29 02:30:42 PDT 2014


On Saturday, 29 March 2014 at 05:01:14 UTC, Gary Miller wrote:
> I've been reading through and trying various code fragments to 
> pull the system date/time in and format it as a string for 
> display on the console.
>
> I only need it down to the second and from my current machine 
> so I don't need to use any of the more accurate clocks used for 
> benchmarking right now.

This is the easiest way i know.

import std.stdio;
import std.datetime;

void main(string[] args)
{
	writeln(Clock.currTime.toLocalTime.toSimpleString);
}

> It would be nice if all the primitive datatypes had a 
> consistent toString cast or function but since I'm not seeing 
> those I guess writeFormatted must be the preferred way of 
> getting values into string format.

See std.conv and the 'to' function.

data.to!(string)
data.to!(int)
etc...

http://dlang.org/phobos/std_conv.html#.to


More information about the Digitalmars-d-learn mailing list