Page 1 of 1

Format Date with Python

Posted: Fri Sep 23, 2022 3:50 pm
by sarnusch
Hello,
I am having struggle with Python in RoboTask and formatting a date.
With method strftime() I want to get the day and month of the actual date as a decimal number.
The code ist: %-d and %-m.

With the following code in RoboTask I get an error:

Code: Select all

from datetime import datetime
print(datetime.today().strftime('%Y,%-m,%-d'))

[STDOUT] Traceback (most recent call last):
[STDOUT] File "C:\Users\robotask\AppData\Local\Temp\2\0zc6oojxecVi9TfbiuwVJVBJ47EVMAUT.py", line 3, in <module>
[STDOUT] print(datetime.today().strftime('%Y,%-m,%-d'))
[STDOUT] ValueError: Invalid format string
Exit code: 1
I already installed Version 3.10.7 of Python and added the path to this python interpreter in RoboTask but the result is still the same.
Am I missing something?

Re: Format Date with Python

Posted: Fri Sep 23, 2022 8:23 pm
by Oleg
Use this expression

Code: Select all

from datetime import datetime
print(datetime.today().strftime('%Y,%#m,%#d'))
Read here, please: https://strftime.org/
The Python docs contain all the format codes that the C standard (1989 version) requires, and these work on all platforms with a standard C implementation. Note that the 1999 version of the C standard added additional format codes. These include codes for non-zero-padded numbers, that can be obtained by appending a dash (-) (UNIX) or hash (#) (Windows) after the percent (%) sign.
python.png
python.png (19.13 KiB) Viewed 1700 times