How to create a DateTime equal to 15 minutes ago?

后端 未结 9 976
庸人自扰
庸人自扰 2020-12-01 05:56

I need to create a DateTime object that represents the current time minus 15 minutes.

相关标签:
9条回答
  • 2020-12-01 06:33

    If you are using time.time() and wants timestamp as output

    Simply use

    CONSTANT_SECONDS = 900 # time  in seconds (900 seconds = 15 min)
    
    current_time = int(time.time())
    time_before_15_min = current_time - CONSTANT_SECONDS
    

    You can change 900 seconds as per your required time.

    0 讨论(0)
  • 2020-12-01 06:35
    from datetime import timedelta    
    datetime.datetime.now() - datetime.timedelta(0, 900)
    
    Actually 900 is in seconds. Which is equal to 15 minutes. `15*60 = 900`
    
    0 讨论(0)
  • 2020-12-01 06:39

    import datetime and then the magic timedelta stuff:

    In [63]: datetime.datetime.now()
    Out[63]: datetime.datetime(2010, 12, 27, 14, 39, 19, 700401)
    
    In [64]: datetime.datetime.now() - datetime.timedelta(minutes=15)
    Out[64]: datetime.datetime(2010, 12, 27, 14, 24, 21, 684435)
    
    0 讨论(0)
提交回复
热议问题