Recent

Author Topic: DateTime  (Read 5254 times)

seghele0

  • Full Member
  • ***
  • Posts: 173
DateTime
« on: September 22, 2021, 04:43:01 pm »
Code: Pascal  [Select][+][-]
  1. ilename := 'Wiezen-' + FormatDateTime('ddmmyyyy',Date)+ '.xlsx';
  2. workbook.WriteToFile(filename, sfOOXML, true);  
  3.  

Sorry to bother you again. The above code works perfectly, but I would like to add the time.

Following code doesn't work:
Code: Pascal  [Select][+][-]
  1. filename := 'Wiezen-' + FormatDateTime('ddmmyyyy'+'hh:mm', Date) + '.xlsx';
Please help.

CCRDude

  • Hero Member
  • *****
  • Posts: 596
Re: DateTime
« Reply #1 on: September 22, 2021, 04:53:34 pm »
The ":" is not allowed as part of the filename e.g. on Windows. It's only allowed in second place of a complete path, right after the drive letter (on Windows).

Just pick a different separator, like a dash?

BobDog

  • Sr. Member
  • ****
  • Posts: 394
Re: DateTime
« Reply #2 on: September 22, 2021, 05:02:13 pm »

This seems to work.
Code: Pascal  [Select][+][-]
  1.  
  2. Uses sysutils;
  3.  
  4. Begin
  5.   Writeln ('Date and Time : ',FormatDateTime('c',now));
  6.   readln;
  7. End.

wp

  • Hero Member
  • *****
  • Posts: 11857
Re: DateTime
« Reply #3 on: September 22, 2021, 05:25:05 pm »
Code: Pascal  [Select][+][-]
  1. filename := 'Wiezen-' + FormatDateTime('ddmmyyyy-hhmm', Now) + '.xlsx';

As already noted, don't use a colon as time separator, it is forbidden (at least on Windows, not sure about Linux - on Windows it is reserved for the separator after the disk drive, e.g. c:\windows...).

And when you want to store the time, use the function Now to retrieve the current complete time - Date would only return the date part, and the time part would always be shown as '00:00' in the filename.
« Last Edit: September 22, 2021, 08:02:14 pm by wp »

CCRDude

  • Hero Member
  • *****
  • Posts: 596
Re: DateTime
« Reply #4 on: September 22, 2021, 06:15:39 pm »

This seems to work.
Because the string is not used as a filename.

BobDog

  • Sr. Member
  • ****
  • Posts: 394
Re: DateTime
« Reply #5 on: September 22, 2021, 07:54:06 pm »

This works in Windows:
Code: Pascal  [Select][+][-]
  1.  
  2. Uses sysutils;
  3.  
  4. procedure savefile(s:ansistring ;filename:ansistring);
  5.         var
  6.         f:textfile;
  7.         begin
  8.         AssignFile(f,filename);
  9.         Rewrite(f);
  10.         writeln(f,s);
  11.         closefile(f);
  12.         end;
  13.  
  14.         procedure replacechar(var s:ansistring;replace:char;withthis:char);
  15.         var
  16.         n:int32;
  17.         begin
  18.           for n:=1 to length(s) do if (s[n]=replace) then s[n]:=withthis;
  19.         end;
  20.  
  21.  var filename:ansistring;
  22.  
  23. Begin
  24.   filename:= DateTimeToStr(now)+'.xlsx';
  25.   replacechar(filename,':','.');
  26.   replacechar(filename,'/','-');
  27.   writeln('filename = ',filename);
  28.   savefile('Hello',filename);
  29.   writeln(fileexists(filename));
  30.  
  31.   deletefile(filename);
  32.   writeln(fileexists(filename));
  33.   readln;
  34. End.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: DateTime
« Reply #6 on: September 23, 2021, 08:56:03 am »
The ":" is not allowed as part of the filename e.g. on Windows. It's only allowed in second place of a complete path, right after the drive letter (on Windows).

And for alternate data streams on file systems that support them (NTFS and ReFS): C:\whatever\myfile.txt:myADS ;)

seghele0

  • Full Member
  • ***
  • Posts: 173
Re: DateTime
« Reply #7 on: September 23, 2021, 12:39:21 pm »
This works fine in Windows10 and in Linux with 'Wine':
Code: Pascal  [Select][+][-]
  1.     varFilename := 'Wiezen-'+FormatDateTime('yyyy-mm-dd__hh.mm', now)+'.xlsx';
    varWorkbook.WriteToFile(varFilename, sfOOXML, true);[/code]
or
Code: Pascal  [Select][+][-]
  1.     varFilename := 'Wiezen-'+FormatDateTime('dd-mm-yyyy__hh.mm', now)+'.xlsx';

Thanks for the support.
 :)


 

TinyPortal © 2005-2018