简体   繁体   中英

Print out datetime as DD-MM-YYYY

I'm making a program that will print out a set of savings for tomorrow only, and i wanted to specify what date tomorrow was, which i did like this:

Tomorrow = datetime.date.today() + datetime.timedelta(days=1)

Which gives the outpu:

2016-04-11

as you can see, it does give tomorrows date like planned, except it does it in the YYYY-MM-DD format, which is my issue. is there a way to have tomorrows date printed in the DD-MM-YYYY format? Cheers

Tomorrow.strftime('%d-%m-%Y') should sort you. Hope that helped.

Here is my answer:

dt = "9'/30'/2017  16:40:09"           # assume dt is the datetime variable
date, space, time = dt.partition(' ')  # use dt.partition to separate date and time by space
mm, slash, str = date.partition('/')   # use date.partition to separate mm and str by slash, mm is found
dd, slash, yyyy = str.partition('/')   # use str.partition to separate dd and yyyy by slash, dd and yyyy are found
new_date = dd + '/' + mm + '/' + yyyy  # set new_date in any desired format 
# new_date = 30/9/2017

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM