Thread overview
Datetime format?
January 18
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);
}
January 18
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



January 18
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.

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


January 19

On Thursday, 18 January 2024 at 23:26:42 UTC, zoujiaqing wrote:

>
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);
}

C++ Std library exmaple code:

// 2019-12-20 19:35:12
std::cout << std::put_time(locNow, "%Y-%m-%d %H:%M:%S") << std::endl;
January 18
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



January 18
On Thu, Jan 18, 2024 at 11:58:32PM +0000, 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);
> > > }
> > > ```
[...]
> So shame! The standard library doesn't have date formatting.
[...]

It's easy to write your own:

````d
import std;

void main() {
	auto curTime = Clock.currTime;
	auto dt = cast(DateTime) curTime;
	auto fmtTime = format("%04d-%02d-%02d %02d:%02d:%02d",
		dt.year, dt.month, dt.day, dt.hour, dt.minute,
		dt.second);
	writeln(fmtTime);
}
````

Output:
	2024-01-18 16:21:51

You have maximum flexibility to format it however you like.


T

-- 
Computers aren't intelligent; they only think they are.
January 20
On Friday, 19 January 2024 at 00:22:48 UTC, H. S. Teoh wrote:
> On Thu, Jan 18, 2024 at 11:58:32PM +0000, 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);
>> > > }
>> > > ```
> [...]
>> So shame! The standard library doesn't have date formatting.
> [...]
>
> It's easy to write your own:
>
> ````d
> import std;
>
> void main() {
> 	auto curTime = Clock.currTime;
> 	auto dt = cast(DateTime) curTime;
> 	auto fmtTime = format("%04d-%02d-%02d %02d:%02d:%02d",
> 		dt.year, dt.month, dt.day, dt.hour, dt.minute,
> 		dt.second);
> 	writeln(fmtTime);
> }
> ````
>
> Output:
> 	2024-01-18 16:21:51
>
> You have maximum flexibility to format it however you like.
>
>
> T

Thank you.