Python:Invalid format string

from datetime import datetime

# Create your views here.
with open('D:/msgdata.txt', 'a+')as f:
    f.write('{}--{}--{}--{}--\n'.format(userB, userA, msg, time.strftime("%y-%m-%d %h:%m:%s")))

主要是时间上出了错time.strftime("%y-%m-%d %h:%m:%s")

经过测试,同样是年份

%y    18

%Y   2018

同样是月份

%m   08

%M    47 会返回现在是本世纪第多少个月

同样是天数

%d   07

%D  08/07/18  会返回斜杠划开的年月日

时分秒:

%H:%M:%S    23:48:56 可以正常显示

%h:%m:%s   会直接报错,应该是小写m会和前边表示月份的m相冲突

应该改为("%Y-%m-%d %H:%M:%S")

猜你喜欢

转载自blog.csdn.net/weixin_42557907/article/details/81490683