python - How to convert tomorrows (at specific time) date to a timestamp -
how can create timestamp next 6 o'clock, whether that's today or tomorrow?
i tried datetime.datetime.today() , replace day +1 , hour = 6 couldnt convert timestamp.
need help
to generate timestamp tomorrow @ 6 am, can use following. creates datetime object representing current time, checks see if current hour < 6 o'clock or not, creates datetime object next 6 o'clock (including adding incrementing day if necessary), , converts datetime object timestamp
from datetime import datetime, timedelta import time # today's datetime dtnow = datetime.now() # create datetime variable 6 dt6 = none # if today's hour < 6 if dtnow.hour < 6: # create date object today's year, month, day @ 6 dt6 = datetime(dtnow.year, dtnow.month, dtnow.day, 6, 0, 0, 0) # if today past 6 am, increment date 1 day else: # 1 day duration add day = timedelta(days=1) # generate tomorrow's datetime tomorrow = dtnow + day # create new datetime object using tomorrow's year, month, day @ 6 dt6 = datetime(tomorrow.year, tomorrow.month, tomorrow.day, 6, 0, 0, 0) # create timestamp datetime object timestamp = time.mktime(dt6.timetuple()) print(timestamp)
Comments
Post a Comment