Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
390 views
in Technique[技术] by (71.8m points)

datetime - how to add 1 day to given date in python

I want to add one day to the date given as argument for a python script

I mean a date will be passed as follows while calling the script python -d mm/dd/yyyy code:

 parser = argparse.ArgumentParser(description='XYZ')
 parser.add_argument("-d","--date",required=True,dest="help", help="help")

I want to add one day to given day I'm trying this :

check_date = args.date
check_pass_date =datetime.check_date + datetime.timedelta(days=1)

this check_pass_date I later want to use as argument for function I'm getting following error for this: type object 'datetime.datetime' has no attribute 'check_date'

Is this reg the importing of module or what? How can I add one day to given date?

question from:https://stackoverflow.com/questions/65933477/how-to-add-1-day-to-given-date-in-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

We can convert the current time in seconds to a date object, and format it to a string very conveniently using date.strftime(format):

import time
from datetime import date

def get_future_date(days: int) -> str:
    futureday = datetime.date.fromtimestamp(time.time() + (86400 * days))
    return futureday.strftime("%m/%d/%Y")

print(get_future_date(1))

Here we take the current time using time.time(), and add x days to that time by multiplying the amount of days passed to the function by the amount of seconds in a day (86400)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...