Python timestamp from day, month, year

Question:

Is it possible to create a UNIX timestamp in Python (number of seconds) with only day, month and year from a date object? I’m essentially looking for what the timestamp would be at midnight (hour, minute and second would be 0).

Thanks!

Asked By: charliesneath

||

Answers:

>>> import time
>>> import datetime
>>> dt = datetime.datetime.strptime('2012-02-09', '%Y-%m-%d')
>>> time.mktime(dt.timetuple())
1328774400.0

–OR–

>>> dt = datetime.datetime(year=2012, month=2, day=9)
>>> time.mktime(dt.timetuple())
1328774400.0

For other Time types like Hour and Second go here:
https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior

Answered By: sberry

You could do it by getting the number of seconds between the date and the epoch (1 January 1970):

from datetime import datetime

epoch = datetime(1970, 1, 1)
d = datetime(2012, 2, 10)

print (d - epoch).total_seconds()
Answered By: jcollado

You can also use datetime.combine()

>>> import datetime
>>> import time
>>> date1 = datetime.date(year=2012,day=02,month=02)
>>> date2 = datetime.datetime.combine(date1,datetime.time())
>>> date2
datetime.datetime(2012, 2, 2, 0, 0)
>>> tstamp = time.mktime(date2.timetuple())
>>> tstamp
1328121000.0

The result depends on local timezone (in this case IST). I hope somebody can point me on how to get GMT result

Answered By: RedBaron
import time
from datetime import datetime


def from_unix_stamp_to_datetime_object(date_int):
    """
    :param date_int: unix timestamp integer
    :return: date time object
    example
    from_unix_stamp_to_datetime_object(1679899329):
    return: 2023-03-27 08:42:09 <class 'datetime.datetime'>
    """
    return datetime.fromtimestamp(date_int)


def from_unix_stamp_to_datetime_as_string(date_int, string_mask='%Y-%m-%d %H:%M:%S'):
    """
    :param date_int: unix timestamp integer
    :param string_mask: string date structure
    :return: date time in string format
    ex.
    from_unix_stamp_to_datetime_as_string(1679899329):
    return: 2023-03-27 08:42:09 <class 'str'>
    """
    return datetime.fromtimestamp(date_int).strftime(string_mask)


def from_date_string_to_datetime_object(date_string, dt_format="%Y-%m-%d %H:%M:%S"):
    """
    :param date_string: ex. 2023-03-27 08:42:09 <class 'str'>
    :param dt_format:
        %Y-%m-%d %H:%M:%S or
        %Y-%m-%d %H:%M:%S.%f for format with milliseconds 2023-03-27 08:42:09.000
    :return: ex. 2023-03-27 08:42:09 <class 'datetime.datetime'>
    """
    return datetime.strptime(date_string, dt_format)


def from_datetime_object_to_unix_timestamp(datetime_object):
    """
    :param datetime_object: ex. 2023-03-27 08:42:09 <class 'datetime.datetime'>
    :return: ex. 1679899329 <class 'int'>
    """
    return round(time.mktime(datetime_object.timetuple()))


def from_single_parameters_to_datetime_object(year, day, month, hour=0, minute=3, second=0, microsecond=0):
    """
    :return: datetime object
    ex.:
    from_single_parameters_to_datetime_object(year=2023, day=2, month=2, hour=0, minute=3, second=0, microsecond=0)
    2023-02-02 00:03:00 <class 'datetime.datetime'>
    """
    return datetime(year=year, day=day, month=month, hour=hour, minute=minute, second=second, microsecond=microsecond)
Answered By: sch
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.