Datetime format?

H. S. Teoh hsteoh at qfbox.info
Fri Jan 19 00:22:48 UTC 2024


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.


More information about the Digitalmars-d-learn mailing list