www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - [your code here]

reply Hello <hello world.com> writes:
auto today()
{
     import std.datetime;
     auto d = cast(DateTime) Clock.currTime();
     import std.format: format;
     return format("%04d/%02d/%02d", d.year, d.month, d.day);
}

void main()
{
     import std.stdio;
     today.writeln;
}
May 03
parent reply Bastiaan Veelo <Bastiaan Veelo.net> writes:
On Saturday, 4 May 2019 at 03:15:59 UTC, Hello wrote:
 auto today()
 {
     import std.datetime;
     auto d = cast(DateTime) Clock.currTime();
     import std.format: format;
     return format("%04d/%02d/%02d", d.year, d.month, d.day);
 }

 void main()
 {
     import std.stdio;
     today.writeln;
 }
The cast is unnecessary, and there is writefln: void main() { import std.datetime, std.stdio; auto d = Clock.currTime(); writefln("%04d/%02d/%02d", d.year, d.month, d.day); } https://run.dlang.io/is/wT6Cn7
May 04
next sibling parent Russel Winder <russel winder.org.uk> writes:
On Sat, 2019-05-04 at 08:06 +0000, Bastiaan Veelo via Digitalmars-d wrote:
 [=E2=80=A6]
=20
 void main()
 {
      import std.datetime, std.stdio;
      auto d =3D Clock.currTime();
      writefln("%04d/%02d/%02d", d.year, d.month, d.day);
 }
"%04d-%02d-%02d" for ISO8601 compliance. --=20 Russel. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D Dr Russel Winder t: +44 20 7585 2200 41 Buckmaster Road m: +44 7770 465 077 London SW11 1EN, UK w: www.russel.org.uk
May 04
prev sibling next sibling parent Jonathan M Davis <newsgroup.d jmdavisprog.com> writes:
On Saturday, May 4, 2019 2:06:59 AM MDT Bastiaan Veelo via Digitalmars-d 
wrote:
 On Saturday, 4 May 2019 at 03:15:59 UTC, Hello wrote:
 auto today()
 {

     import std.datetime;
     auto d = cast(DateTime) Clock.currTime();
     import std.format: format;
     return format("%04d/%02d/%02d", d.year, d.month, d.day);

 }

 void main()
 {

     import std.stdio;
     today.writeln;

 }
The cast is unnecessary, and there is writefln: void main() { import std.datetime, std.stdio; auto d = Clock.currTime(); writefln("%04d/%02d/%02d", d.year, d.month, d.day); } https://run.dlang.io/is/wT6Cn7
The cast isn't necessary, but it's more efficient. Internally, it basically doing the equivalent of casting to DateTime every time you ask for the pieces of the date/time such as the year or hour. So, whether is casting is better or not depends on whether you want shorter code or faster code. - Jonathan M Davis
May 09
prev sibling parent Jonathan M Davis <newsgroup.d jmdavisprog.com> writes:
On Saturday, May 4, 2019 5:17:57 AM MDT Russel Winder via Digitalmars-d 
wrote:
 On Sat, 2019-05-04 at 08:06 +0000, Bastiaan Veelo via Digitalmars-d wrote:
 […]

 void main()
 {

      import std.datetime, std.stdio;
      auto d = Clock.currTime();
      writefln("%04d/%02d/%02d", d.year, d.month, d.day);

 }
"%04d-%02d-%02d" for ISO8601 compliance.
If that's what you want, then just cast to Date and call toISOExtString() on it. - Jonathan M Davis
May 09