Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
May 04, 2019 [your code here] | ||||
---|---|---|---|---|
| ||||
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 04, 2019 Re: [your code here] | ||||
---|---|---|---|---|
| ||||
Posted in reply to Hello | 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, 2019 Re: [your code here] | ||||
---|---|---|---|---|
| ||||
Posted in reply to Bastiaan Veelo Attachments:
| 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. -- Russel. =========================================== 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 09, 2019 Re: [your code here] | ||||
---|---|---|---|---|
| ||||
Posted in reply to Bastiaan Veelo | 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, 2019 Re: [your code here] | ||||
---|---|---|---|---|
| ||||
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
|
Copyright © 1999-2021 by the D Language Foundation