www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Date Formating

reply Vino <vino.bheeman hotmail.com> writes:
Hi All,

   Request out help on date formatting, I have code which output 
the date and time as below , i we need it without the last few 
numbers.,ie "YYYY-MMM-DD HH:MM:SI"

Output : 2017-Sep-06 16:06:42.7223837
Required Output : 2017-Sep-06 16:06:42

From,
Vino.B
Dec 12 2017
next sibling parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Tuesday, 12 December 2017 at 15:56:59 UTC, Vino wrote:
   Request out help on date formatting, I have code which output 
 the date and time as below , i we need it without the last few 
 numbers.,ie "YYYY-MMM-DD HH:MM:SI"
Just slice it off. x[0 .. x.lastIndexOf("."];
Dec 12 2017
parent Vino <vino.bheeman hotmail.com> writes:
On Tuesday, 12 December 2017 at 16:01:49 UTC, Adam D. Ruppe wrote:
 On Tuesday, 12 December 2017 at 15:56:59 UTC, Vino wrote:
   Request out help on date formatting, I have code which 
 output the date and time as below , i we need it without the 
 last few numbers.,ie "YYYY-MMM-DD HH:MM:SI"
Just slice it off. x[0 .. x.lastIndexOf("."];
Hi, I tried it but no luck, as the output is a function return in type SysTime. From, Vino.B
Dec 12 2017
prev sibling next sibling parent Steven Schveighoffer <schveiguy yahoo.com> writes:
On 12/12/17 10:56 AM, Vino wrote:
 Hi All,
 
    Request out help on date formatting, I have code which output the 
 date and time as below , i we need it without the last few numbers.,ie 
 "YYYY-MMM-DD HH:MM:SI"
 
 Output : 2017-Sep-06 16:06:42.7223837
 Required Output : 2017-Sep-06 16:06:42
 
You need to extract the date components and print them separately. There is no mechanism to format a SysTime (and it's a hairy subject to be sure, every locale is different). -Steve
Dec 12 2017
prev sibling next sibling parent Jonathan M Davis <newsgroup.d jmdavisprog.com> writes:
On Tuesday, December 12, 2017 15:56:59 Vino via Digitalmars-d-learn wrote:
 Hi All,

    Request out help on date formatting, I have code which output
 the date and time as below , i we need it without the last few
 numbers.,ie "YYYY-MMM-DD HH:MM:SI"

 Output : 2017-Sep-06 16:06:42.7223837
 Required Output : 2017-Sep-06 16:06:42
If you want to strip off the fractional seconds, then just zero them out. e.g. sysTime.fracSecs = Duration.zero; The to*String functions of SysTime don't display trailing zeroes and don't display the decimal point if the fractional seconds are zero. - Jonathan M Davis
Dec 12 2017
prev sibling parent reply codephantom <me noyb.com> writes:
On Tuesday, 12 December 2017 at 15:56:59 UTC, Vino wrote:
 Hi All,

   Request out help on date formatting, I have code which output 
 the date and time as below , i we need it without the last few 
 numbers.,ie "YYYY-MMM-DD HH:MM:SI"

 Output : 2017-Sep-06 16:06:42.7223837
 Required Output : 2017-Sep-06 16:06:42

 From,
 Vino.B
just playing with this... // ---------- module test; void main() { import std.stdio; writeln( GetFmtDate() ); // e.g: 2017-Dec-13 13:30:23 } string GetFmtDate() { import std.datetime; import std.ascii : toUpper; import std.conv : to; import std.string : format; auto d = Clock.currTime(); string fmtMonth = toUpper(to!string(d.month)[0]) ~ to!string(d.month)[1..$]; return format("%04s-%s-%02s %02s:%02s:%02s", (d.year), fmtMonth, (d.day), (d.hour), (d.minute), (d.second) ); } // --------------
Dec 12 2017
parent reply Jonathan M Davis <newsgroup.d jmdavisprog.com> writes:
On Wednesday, December 13, 2017 02:34:12 codephantom via Digitalmars-d-learn 
wrote:
 On Tuesday, 12 December 2017 at 15:56:59 UTC, Vino wrote:
 Hi All,

   Request out help on date formatting, I have code which output

 the date and time as below , i we need it without the last few
 numbers.,ie "YYYY-MMM-DD HH:MM:SI"

 Output : 2017-Sep-06 16:06:42.7223837
 Required Output : 2017-Sep-06 16:06:42

 From,
 Vino.B
just playing with this... // ---------- module test; void main() { import std.stdio; writeln( GetFmtDate() ); // e.g: 2017-Dec-13 13:30:23 } string GetFmtDate() { import std.datetime; import std.ascii : toUpper; import std.conv : to; import std.string : format; auto d = Clock.currTime(); string fmtMonth = toUpper(to!string(d.month)[0]) ~ to!string(d.month)[1..$]; return format("%04s-%s-%02s %02s:%02s:%02s", (d.year), fmtMonth, (d.day), (d.hour), (d.minute), (d.second) ); } // --------------
In general, you probably want to cast the SysTime to a DateTime if you're going to do something like that. There's a lot of duplicate work being done if you get each of those properties individually, whereas if you cast to DateTime, then it does the work once, and the properties for DateTime just return the correspending member variables. Sometimes, I think that putting year, month, etc. on SysTime was a mistake, because using thoes properties almost always the wrong thing to do given how much work is duplicated when using them, but at the same time, there are cases where the efficiency doesn't really matter and the simplicity of just grabbing the properties from SysTime is nice. - Jonathan M Davis
Dec 12 2017
parent reply codephantom <me noyb.com> writes:
On Wednesday, 13 December 2017 at 07:35:40 UTC, Jonathan M Davis 
wrote:
 In general, you probably want to cast the SysTime to a DateTime 
 if you're going to do something like that.
yes, I would agree ;-) Of course the intention was not really to just format it the same way as Clock.currTime() does it, but rather to provide a way to more easily customise the format, however one likes, whenever one likes.. e.g.the following small change to the format string would make it return: 20171213_1924_41 (that's more like something I'd use) return format("%04s%02s%02s_%02s%02s_%02s", (d.year), to!(int)(d.month), (d.day), (d.hour), (d.minute), (d.second) );
Dec 13 2017
parent reply Vino <vino.bheeman hotmail.com> writes:
On Wednesday, 13 December 2017 at 08:32:34 UTC, codephantom wrote:
 On Wednesday, 13 December 2017 at 07:35:40 UTC, Jonathan M 
 Davis wrote:
 In general, you probably want to cast the SysTime to a 
 DateTime if you're going to do something like that.
yes, I would agree ;-) Of course the intention was not really to just format it the same way as Clock.currTime() does it, but rather to provide a way to more easily customise the format, however one likes, whenever one likes.. e.g.the following small change to the format string would make it return: 20171213_1924_41 (that's more like something I'd use) return format("%04s%02s%02s_%02s%02s_%02s", (d.year), to!(int)(d.month), (d.day), (d.hour), (d.minute), (d.second) );
Hi All, Request your help on below program on how to format or cast SysTime to DateTime import std.algorithm: filter, map, sort; import std.container.array; import std.file: SpanMode, dirEntries, isDir ; import std.stdio: writeln,writefln; import std.typecons: Tuple, tuple; import std.datetime.systime: SysTime; void main () { auto FFs = ["C:\\Temp\\BACKUP", "C:\\Temp\\\EXPORT"]; Array!(Tuple!(string, SysTime)) Sorted; foreach(d; FFs[]) { auto dFiles = Array!(Tuple!(string, SysTime))(dirEntries(d, SpanMode.shallow).filter!(a => a.isDir).map!(a => tuple(a.name, a.timeCreated))); foreach(i; dFiles[]){ Sorted ~= i; } writefln("%(%-(%-63s %s %)\n%)", Sorted[].sort!((a,b) => a[1] < b[1])); } } Output: C:\Temp\BACKUP\DND3 2017-Sep-05 14:31:00.7037169 C:\Temp\BACKUP\DND5 2017-Sep-05 14:31:00.750517 C:\Temp\EXPORT\DND6 2017-Sep-05 14:31:00.8909172 C:\Temp\BACKUP\dir1 2017-Sep-06 16:06:42.7223837 C:\Temp\EXPORT\dir2 2017-Sep-06 16:06:43.1435864 C:\Temp\BACKUP\dir2 2017-Sep-09 22:44:11.7604069 C:\Temp\BACKUP\dir3 2017-Dec-10 06:56:07.5122231 C:\Temp\BACKUP\t1 2017-Dec-11 04:10:02.6413853 Required Output C:\Temp\BACKUP\DND3 2017-Sep-05 14:31:00 C:\Temp\BACKUP\DND5 2017-Sep-05 14:31:00 C:\Temp\EXPORT\DND6 2017-Sep-05 14:31:00 C:\Temp\BACKUP\dir1 2017-Sep-06 16:06:42 C:\Temp\EXPORT\dir2 2017-Sep-06 16:06:43 C:\Temp\BACKUP\dir2 2017-Sep-09 22:44:11 C:\Temp\BACKUP\dir3 2017-Dec-10 06:56:07 C:\Temp\BACKUP\t1 2017-Dec-11 04:10:02 From, Vino.B
Dec 13 2017
parent Vino <vino.bheeman hotmail.com> writes:
On Wednesday, 13 December 2017 at 17:16:46 UTC, Vino wrote:
 On Wednesday, 13 December 2017 at 08:32:34 UTC, codephantom 
 wrote:
                 [...]
Hi All, [...]
Hi All, Thank you very much , was able to resolve the issue by changing the writefln line as below. Sorted[].sort!((a,b) => a[1] > b[1]).each!(e => writefln!"%-63s %.20s"(e[0], e[1].to!string)); From, Vino.B
Dec 13 2017