Epoch Converter and Date/Time in Python

Quick Navigation

Date Types

A date object represents a date (year, month and day) in an idealized calendar, the current Gregorian calendar indefinitely extended in both directions.

    import time
    from datetime import date

    today = date.today()
    print("Today's Date: "+str(today))
    #The str() method returns a nicely printable representation of a given object.

    d = date(2005, 7, 14)
    print("Date: "+str(d))
    print("Day: "+str(d.day))
    print("Month: "+str(d.month))
    print("Year: "+str(d.year))
    print("ISO Week Day: "+str(d.isoweekday()))



    Today's Date: 2019-02-16
    Date: 2005-07-14
    Day: 14
    Month: 7
    Year: 2005
    ISO Week Day: 4


DateTime Objects

A datetime object is a single object containing all the information from a date object and a time object.

    from datetime import datetime

    #get the datetime of today
    #the datetime class has attributes year, month, day, hour, minute, second, and microsecond
    datetime_object = datetime.now()
    print("datetime_object: ", datetime_object)
    print("datetime_object.year: ",datetime_object.year)
    print("datetime_object.month: ", datetime_object.month)
    print("datetime_object.day: ",datetime_object.day)
    print("datetime_object.hour: ",datetime_object.hour)
    print("datetime_object.minute: ",datetime_object.minute)
    print("datetime_object.second: ",datetime_object.second)
    print("datetime_object.microsecond: ",datetime_object.microsecond)



    datetime_object:  2019-02-16 02:30:10.863942
    datetime_object.year:  2019
    datetime_object.month:  2
    datetime_object.day:  16
    datetime_object.hour:  2
    datetime_object.minute:  30
    datetime_object.second:  10
    datetime_object.microsecond:  863942


Convert from Epoch to Human Readable Date

We can convert timestamp or epoch to human readble date. A Unix timestamp is the number of seconds between a particular date and January 1, 1970 at UTC. You can convert a timestamp to a datetime or date using fromtimestamp() method.

    from datetime import datetime, date

    timestamp = datetime.fromtimestamp(1549401937)
    print("Date =", timestamp)

    #You could also use the date class to call fromtimestamp if you just want the date and not the time
    timestamp2 = date.fromtimestamp(1549401937)
    print("Date =", timestamp2)



    Date = 2019-02-05 21:25:37
    Date = 2019-02-05


Convert from Date or DateTime to Epoch

We can convert the date or datetime to a timestamp.

    import time
    from datetime import datetime, date

    #get the timestamp for this date
    d = date(2015,1,5)
    unixtime = time.mktime(d.timetuple())
    print("Timestamp of date 1-5-2015: ", unixtime)

    #get the current timestamp
    d2 = datetime.today()
    unixtime2 = time.mktime(d2.timetuple())
    print("Timestamp of now: ", unixtime2)



    Timestamp of date 1-5-2015:  1420416000.0
    Timestamp of now:  1550287208.0


Programming Languages: