简体   繁体   中英

How to only output the days (without hours, minutes, and seconds) while using time delta?

I have a function:

def diffDate(YYYY,MM,DD):
    tgl = date(YYYY,MM,DD)
    skrg = datetime.date(datetime.now())
    if tgl <= skrg:
        diffDate = skrg - tgl
    elif tgl >= skrg:
        diffDate = tgl - skrg
    return diffDate

The output will be "x days, 0:00:00". How to only output the days without the hours, minutes, and seconds? And can I multiple the day difference with integer without taking the 'days' ? *Sorry for my bad english

You can do multiple steps in one:

datetime import date

def absoluteDaysToNow(yyyy, mm, dd):
  delta = date.today() - date(yyyy, mm, dd)
  return abs( delta.days )

See: python docs, built-in function abs() , date example: days difference

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