开发者

How to get formatted date time in python

开发者 https://www.devze.com 2023-03-06 00:38 出处:网络
I want my Linux Filename like this May-01-0340AM-2011.tar How can i get the date variable formatted like abov开发者_运维百科e in Python

I want my Linux Filename like this

May-01-0340AM-2011.tar

How can i get the date variable formatted like abov开发者_运维百科e in Python

IN bash i write

date1=$(date +"%b-%d-%I%M%p-%G")


You can use the same formatting string in strftime on a datetime object:

>>> import datetime
>>> datetime.datetime.now().strftime('%b-%d-%I%M%p-%G')
'May-16-0245PM-2011'

Incidentally, I'd just like to put a word in for the joy of ISO-8601 date formatting


Same formatting, using strftime():

>>> import datetime
>>> datetime.datetime.now().strftime('%b-%d-%I%M%p-%G')
'May-16-1050PM-2011'

To get your filename, it's as simple as:

 >>> datetime.datetime.now().strftime('%b-%d-%I%M%p-%G') + '.tar'
'May-16-1050PM-2011.tar'
0

精彩评论

暂无评论...
验证码 换一张
取 消