Format Date with Python

Discuss RoboTask here
Post Reply
sarnusch
Posts: 35
Joined: Thu Apr 28, 2022 9:05 am

Format Date with Python

Post 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?
Attachments
python1.png
python1.png (26.74 KiB) Viewed 1586 times
python2.png
python2.png (25 KiB) Viewed 1586 times
Oleg
Site Admin
Posts: 3000
Joined: Thu Jan 01, 1970 1:00 am
Contact:

Re: Format Date with Python

Post 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 1578 times
Oleg Yershov
Post Reply