简体   繁体   English

如何使用剥离时间'%Y-%m-%d%H:%M:%S'减去-1分钟。

[英]How to use striptime '%Y-%m-%d %H:%M:%S' minus - 1 minute.?

I am trying to basically use striptime that gives me format of '%Y-%m-%d %H:%M:%S' etc '2019-05-03 09:00:00' and what I am trying to achieve is that I want to take that time - 1 minute so the output should be - 2019-05-03 08:59:00 我正在尝试使用基本上给我'%Y-%m-%d%H:%M:%S'等'2019-05-03 09:00:00' ,我正在尝试实现是我要花那个时间-1分钟所以输出应该是2019-05-03 08:59:00

what I have done so far is 到目前为止,我所做的是

    date_time = '2019-05-03 09:00:00'
    target = datetime.strptime(date_time, '%Y-%m-%d %H:%M:%S')

    now = datetime.now()

    delta = target - now

    if delta > timedelta(0):
        print('Will sleep {} : {}'.format(date_time, delta))
        time.sleep(delta.total_seconds())

and I am not sure how I can make the function to do - 1 minute. 我不确定如何使该功能生效-1分钟。 I am not sure if its even possible? 我不确定是否可能?

It appears you meant to use the formatted datetime variable called target in your if clause but instead you used the string representation of that date called date_time . 看来您打算在if子句中使用格式为datetime的变量target ,但是您使用了该日期的字符串表示形式date_time

Use the datetime object instead and substract timedelta(minutes=1) 改用datetime对象并减去timedelta(minutes=1)

Working example: 工作示例:

date_time = '2019-05-03 09:00:00'
target = datetime.strptime(date_time, '%Y-%m-%d %H:%M:%S')

now = datetime.now()

delta = target - now

if delta > timedelta(0):
    target = target - timedelta(minutes=1)
    print('Will sleep {} : {}'.format(target, delta))
    time.sleep(delta.total_seconds())

If you would like to use the current time to determine your output 如果您想使用当前时间来确定您的输出

import datetime, time
from datetime import timedelta

daytime = '2019-05-03 09:00:00'
target = datetime.datetime.strptime(daytime, '%Y-%m-%d %H:%M:%S')
print (datetime.datetime.now())

now = datetime.datetime.now()
new_time = target-now

if  new_time > datetime.timedelta(0):
        print('Will sleep {} : {}'.format(daytime, new_time))
        time.sleep(new_time.total_seconds())

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在格式为 '%Y-%m-%dT%H:%M:%S' 的变量中减去 1 分钟 - How to minus 1 minute in a variable with format as '%Y-%m-%dT%H:%M:%S' 如何仅保留已记录时间序列的小时:分钟:第二部分? (如何将 %Y-%m-%d %H:%M:%S 转换为仅 %H:%M:%S ?) - How to keep only the Hour:Minute:Second part of recorded time series? (How to convert %Y-%m-%d %H:%M:%S to only %H:%M:%S ?) 如何将日期时间从十进制转换为“%y-%m-%d%H:%M:%S”给定时间原点? - How to convert datetime from decimal to “%y-%m-%d %H:%M:%S” given the time origin? ValueError: 时间数据“无”与格式“%Y-%m-%d %H:%M:%S”不匹配 - ValueError: time data 'None' does not match format '%Y-%m-%d %H:%M:%S' 时间数据与格式 '%Y-%m-%d %H:%M:%S' 不匹配 - time data does not match format '%Y-%m-%d %H:%M:%S' ValueError:时间数据“ 140120 1520”与格式“%Y-%m-%d%H:%M:%S”不匹配 - ValueError: time data '140120 1520' does not match format '%Y-%m-%d %H:%M:%S' ValueError: 时间数据与格式 '%Y-%m-%d %H:%M:%S.%f' 不匹配 - ValueError: time data does not match format '%Y-%m-%d %H:%M:%S.%f' 出现错误“ValueError:时间数据''与格式'%Y-%m-%d %H:%M:%S'不匹配” - Getting error "ValueError: time data '' does not match format '%Y-%m-%d %H:%M:%S'" 将 Twitter 新日期格式转换为日期时间 ( %Y-%m-%d %H:%M%S' ) - Convert Twitter New Date Format to Date Time ( %Y-%m-%d %H:%M%S' ) ValueError:“2”是格式“%Y-%m-%d%20%H:%M:%S”的错误指令 - ValueError: '2' is a bad directive in format '%Y-%m-%d%20%H:%M:%S'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM