1

I'm attempting to create a datetime variable for the start of the day. Ultimately, so I can pass it into various queries (like example below) via an f string.

import datetime 

start_datetime = datetime.datetime.today()

query = f""" (SELECT *
             FROM   TABLE_NAME
             WHERE  datetime_var >= '{start_datetime}'
             )"""

I'm having trouble working out how to get zero hours into the datetime/timestamp.

This is what I've tried:

from pyspark.sql.functions import to_timestamp

# attaching '00:00:00' to the date
start_date = datetime.date.today()
start_dt = to_timestamp(str(start_date)+' 00:00:00')

print(start_dt)
>>> Column<b'to_timestamp(`2020-02-10 00:00:00`)'>

This doesn't resolve into a value. I've not been able to work out what else to try.

How can I get the midnight of the current day timestamp?

Thanks in advance.

bumblebear
  • 83
  • 8

2 Answers2

1

Using what you already imported :

import datetime 
from pyspark.sql.functions import to_timestamp

start_datetime = str(datetime.date.today().strftime('%Y-%m-%d %H:%M:%S'))
query = f""" (SELECT *
             FROM   TABLE_NAME
             WHERE  datetime_var >= '{start_datetime}'
             )"""
print(start_datetime)
>>> 2020-02-10 00:00:00
Axel R.
  • 1,141
  • 7
  • 22
0

Thanks @AnkurChavda for posting that link. Here's how to do it.

import datetime 

# replace hour, minute, second & microsecond values 
start_dt = datetime.datetime.today().replace(hour=0, minute=0, second=0, microsecond=0)

print(start_dt)
>>> 2020-02-10 00:00:00
bumblebear
  • 83
  • 8