如何计算从现在起加上n天的总秒数
How to calculate the total seconds from now plus n days
我想在每周二 12:00pm
发送通知
我想在周二 12:00pm
之前获得未来的准确时间(以秒为单位)
import datetime
from datetime import date , timedelta
today = datetime.date.today()
Tuesday = datetime.timedelta( (1-today.weekday()) % 7 ) + timedelta(hours=12)
seconds_to_call = Tuesday.total_seconds()
print(seconds_to_call)
# Gives exactly 5 days in seconds 432,000 + timedelta(hours=12) 43200
# 以秒为单位给出 5 天 432,000 + timedelta(hours=12) 43200
如何获得星期二 00:00 加上 12 小时的时间增量(小时=12)43200
那么我会在星期二中午 12 点
谢谢
第一题
I want to get an exact time in the future in seconds until Tuesday at 12:00pm
我想你想要从今天
到星期二12:00的确切时间(以秒为单位)
import datetime
def relative_date(reference, weekday, timevalue):
hour, minute = divmod(timevalue, 1)
minute *= 60
days = reference.weekday()+(reference.weekday() - weekday)
return (reference + datetime.timedelta(days=days)).replace(hour=int(hour), minute=int(minute), second=0, microsecond=0)
today = datetime.datetime.now()
Tuesday = relative_date(today, 1, 12)
seconds_to_call = (Tuesday - today).total_seconds()
print(seconds_to_call)
# OUTPUT: 405778.097769
你必须计算今天和下周二这两个日期之间的差异。函数 relative_date
计算下周二的确切日期并将小时设置为 12:00:00.000
.
第二个问题
I want to run the program get the number of seconds until 00:00 then had the number of seconds until 12 (43,200)
你可以这样做:
import datetime
def relative_date(reference, weekday, timevalue):
hour, minute = divmod(timevalue, 1)
minute *= 60
days = reference.weekday()+(reference.weekday() - weekday)
return (reference + datetime.timedelta(days=days)).replace(hour=int(hour), minute=int(minute), second=0, microsecond=0)
today = datetime.datetime.now()
Tuesday_midnight = relative_date(today, 1, 0)
seconds_to_call = ((Tuesday_midnight - today) + datetime.timedelta(hours=12)).total_seconds()
print(seconds_to_call)
# OUTPUT: 405778.097769
在这种情况下计算到午夜的时间是没有用的,你可以得到total_seconds,如第一题的代码,直接从中午开始。
from datetime import datetime, timedelta
# get the date and time for now
now = datetime.now()
# get the current day at midnight
today = now.replace(hour=0, minute=0, second=0, microsecond=0)
# get Tuesday at midnight
tues = today + timedelta( (1-today.weekday()) % 7 )
# get the seconds from now until Tuesday at midnight
seconds_to_tues_midnight = (tues - now).total_seconds()
# get the seconds from now until Tuesday at noon
seconds_to_tues_noon = seconds_to_tues_midnight + timedelta(hours=12)/timedelta(seconds=1)
作为函数
from typing import Tuple
def time_to_tuesday(now: datetime) -> Tuple[float, float]:
today = now.replace(hour=0, minute=0, second=0, microsecond=0)
tues = today + timedelta( (1-today.weekday()) % 7 )
midnight = (tues - now).total_seconds()
noon = midnight + timedelta(hours=12)/timedelta(seconds=1)
return midnight, noon
time_to_tuesday(datetime.now())
我想在每周二 12:00pm
发送通知我想在周二 12:00pm
之前获得未来的准确时间(以秒为单位)import datetime
from datetime import date , timedelta
today = datetime.date.today()
Tuesday = datetime.timedelta( (1-today.weekday()) % 7 ) + timedelta(hours=12)
seconds_to_call = Tuesday.total_seconds()
print(seconds_to_call)
# Gives exactly 5 days in seconds 432,000 + timedelta(hours=12) 43200
# 以秒为单位给出 5 天 432,000 + timedelta(hours=12) 43200
如何获得星期二 00:00 加上 12 小时的时间增量(小时=12)43200
那么我会在星期二中午 12 点
谢谢
第一题
I want to get an exact time in the future in seconds until Tuesday at 12:00pm
我想你想要从今天
到星期二12:00的确切时间(以秒为单位)import datetime
def relative_date(reference, weekday, timevalue):
hour, minute = divmod(timevalue, 1)
minute *= 60
days = reference.weekday()+(reference.weekday() - weekday)
return (reference + datetime.timedelta(days=days)).replace(hour=int(hour), minute=int(minute), second=0, microsecond=0)
today = datetime.datetime.now()
Tuesday = relative_date(today, 1, 12)
seconds_to_call = (Tuesday - today).total_seconds()
print(seconds_to_call)
# OUTPUT: 405778.097769
你必须计算今天和下周二这两个日期之间的差异。函数 relative_date
计算下周二的确切日期并将小时设置为 12:00:00.000
.
第二个问题
I want to run the program get the number of seconds until 00:00 then had the number of seconds until 12 (43,200)
你可以这样做:
import datetime
def relative_date(reference, weekday, timevalue):
hour, minute = divmod(timevalue, 1)
minute *= 60
days = reference.weekday()+(reference.weekday() - weekday)
return (reference + datetime.timedelta(days=days)).replace(hour=int(hour), minute=int(minute), second=0, microsecond=0)
today = datetime.datetime.now()
Tuesday_midnight = relative_date(today, 1, 0)
seconds_to_call = ((Tuesday_midnight - today) + datetime.timedelta(hours=12)).total_seconds()
print(seconds_to_call)
# OUTPUT: 405778.097769
在这种情况下计算到午夜的时间是没有用的,你可以得到total_seconds,如第一题的代码,直接从中午开始。
from datetime import datetime, timedelta
# get the date and time for now
now = datetime.now()
# get the current day at midnight
today = now.replace(hour=0, minute=0, second=0, microsecond=0)
# get Tuesday at midnight
tues = today + timedelta( (1-today.weekday()) % 7 )
# get the seconds from now until Tuesday at midnight
seconds_to_tues_midnight = (tues - now).total_seconds()
# get the seconds from now until Tuesday at noon
seconds_to_tues_noon = seconds_to_tues_midnight + timedelta(hours=12)/timedelta(seconds=1)
作为函数
from typing import Tuple
def time_to_tuesday(now: datetime) -> Tuple[float, float]:
today = now.replace(hour=0, minute=0, second=0, microsecond=0)
tues = today + timedelta( (1-today.weekday()) % 7 )
midnight = (tues - now).total_seconds()
noon = midnight + timedelta(hours=12)/timedelta(seconds=1)
return midnight, noon
time_to_tuesday(datetime.now())