I am trying to convert now.month to decimal so it would look like 01 and not like 1. The code is:
import datetime
now = datetime.datetime.now()
monthstr = ".%m" % now.month
it works in this line with %d
daystr = "file_%d" % now.day
the error I get is
ValueError: unsupported format character 'm' (0x6d) at index 2
I also tried with the new type {:%m}. The error is the same. Do i miss any libraries? Or is there another way except adding 0 to months from Jan to September
Python v.3.6.3
This is because %d
do not stands for days
. It stands for digits and is a placeholder for numbers when using string formatting.
You should use it for months also.
monthstr = ".%d" % now.month
Since you want the format as 01 and not like 1
, use @user3483203 answer .
You are using string formatting like datetime formatting here. String formatting has its own "mini-language" , and m
is not a valid symbol in this language.
For your purposes, it would be better to use strftime
>>> now.strftime('.%m')
'.07'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.