Datetime format?

Jonathan M Davis newsgroup.d at jmdavisprog.com
Fri Jan 19 00:19:00 UTC 2024


On Thursday, January 18, 2024 4:58:32 PM MST zoujiaqing via Digitalmars-d-
learn wrote:
> On Thursday, 18 January 2024 at 23:43:13 UTC, Jonathan M Davis
>
> wrote:
> > On Thursday, January 18, 2024 4:26:42 PM MST zoujiaqing via
> >
> > Digitalmars-d- learn wrote:
> >> ```D
> >> import std.datetime : Clock, format;
> >> import std.stdio : writeln;
> >>
> >> void main()
> >> {
> >>
> >>      auto currentTime = Clock.currTime;
> >>
> >>      auto formattedTime = currentTime.format("%Y-%m-%d
> >>
> >> %H:%M:%S");
> >>
> >>      writeln("Formatted Time: ", formattedTime);
> >>
> >> }
> >> ```
> >
> > std.datetime does not currently support custom date/time
> > formats. It only supports the ISO format, the ISO Extended
> > format, and Boost's simple time format.
> >
> > // e.g. 20240118T163806.5813052
> > auto iso = time.toISOString();
> >
> > // e.g. 2024-01-18T16:38:06.5813052
> > auto isoExt = time.toISOExtString();
> >
> > // e.g. 2024-Jan-18 16:38:06.5813052
> > auto boostSimple = time.toSimpleString();
> >
> > So, if you want a different format, you'll either need to make
> > one yourself by calling the various properties on SysTime and
> > passing them to something like std.format's format to create a
> > string, or there are several packages on https://code.dlang.org
> > which have functions for doing custom date/time formatting.
> >
> > - Jonathan M Davis
>
> Thank you for your replay.
>
> So shame! The standard library doesn't have date formatting.

It probably should, but it wasn't a priority when std.datetime was written,
and I've never gotten around to adding it.

> for this example "2024-Jan-18 16:38:06.5813052"
> Why use Jan? no 01?
> International standards should all apply numbers.
> like this:
> 2024-01-18 16:38:06.5813052

It uses Jan, because that's Boost's "simple" date/time format. At this
point, I consider it a mistake to have put toSimpleString in there or to
have had toString use toSimpleString instead of toISOExtString, but it's
there because Boost had it with their date/time type.

- Jonathan M Davis





More information about the Digitalmars-d-learn mailing list