Save text file with current date and time filename

Hi,
I would like to create a file on the plc user partition with current date and time as name.
If I simply use DTGetTime_0 it does not work because the string has spaces and undesired characters, so file generation fails.
I eventually used
DTStructureGetTime
to get date and time as structure, but now I have to do a lot of subsequent CONCAT to create the final string for the filename.
Is there any smarter (and faster) way to achieve that?
thanks

Hi,

you could still use DTGetTime_0 and while-loop the returned string to check for any empty spaces or undesired characters with the FIND() function (I assume you mean the hash in datetime string “DT#2025-01-…”).

I think this would reduce the amount of code lines since it’s a loop (and you could easily create the code with ChatGPT :wink: ).

I’m not aware if there any other, easier possibilities.

thanks, I will try that

Hi,

as long as you need the “date and time filename” as readable information, you have to do some coding to format code to transform the datetime information into a filename suitable string.

Nevertheless an additional info about that:
if you don’t need to have the date and time string in a human readable format, for example because you use date and time only to avoid double filenames, then you could use a more easy way.
Since DATE_AND_TIME is just an interpretation of a 4-byte value meaning “seconds since 1.1.1970”, you could

  • memcopy the DATE_AND_TIME information to an UDINT variable
  • transform the UDINT to a STRING

… not human readable, but the string also will change every second :wink:

that would be easy, but as you know requirements are never so easy :joy:
customer wants to have filenames with that format, so it should be human readable

1 Like

Hi @c563049

Can you try this function from this IecString library?

You can format date and time for any requirement.

4 Likes

woooow!!! you saved my day! thank you very much that’s perfect!

just out of curiosity: I understand that we are talking about a string library that will not hurt anyone, but how safe is to use external libraries found on github?

I would categorize the safety of external code this way:

  • For a source-available: as safe as your ability to read and understand the code
  • For a binary-only library: as safe as your trust in that third-party

Best regards

3 Likes