digitalmars.D.learn - Datetime format?
- zoujiaqing (10/10) Jan 18 ```D
- Jonathan M Davis (17/27) Jan 18 std.datetime does not currently support custom date/time formats. It onl...
- zoujiaqing (9/40) Jan 18 Thank you for your replay.
- Jonathan M Davis (9/59) Jan 18 It probably should, but it wasn't a priority when std.datetime was writt...
- H. S. Teoh (21/38) Jan 18 [...]
- zoujiaqing (2/41) Jan 20 Thank you.
- zoujiaqing (7/18) Jan 18 C++ Std library exmaple code:
```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); } ```
Jan 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
Jan 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: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```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
Jan 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:It probably should, but it wasn't a priority when std.datetime was written, and I've never gotten around to adding it.On Thursday, January 18, 2024 4:26:42 PM MST zoujiaqing via Digitalmars-d- learn wrote:Thank you for your replay. So shame! The standard library doesn't have date formatting.```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 Davisfor 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.5813052It 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
Jan 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.
Jan 18
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:Thank you.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
Jan 20
On Thursday, 18 January 2024 at 23:26:42 UTC, zoujiaqing 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); } ```C++ Std library exmaple code: ```CPP // 2019-12-20 19:35:12 std::cout << std::put_time(locNow, "%Y-%m-%d %H:%M:%S") << std::endl; ```
Jan 18