What is Timestamp in Python and How to work with it?

Posted in /  

What is Timestamp in Python and How to work with it?

Vinay Khatri
Last updated on June 10, 2022

    In general, the term timestamp defines a point of registered time when a specific event has occurred. And in computer science when we generally say timestamp, it represents the time in seconds from Unix time(which is also known as the Epoch time or seconds since Epoch) which is 1st January 1970. In this Python tutorial, you will learn what is timestamp in Python and how to work with it. And by the end of this tutorial, you will build a solid understanding of the following topics

    1. What is a timestamp in Python
    2. How to get the current timestamp in Python
    3. How to convert the timestamp to DateTime in Python
    4. How to convert datetime in timestamp in Python
    5. Format timestamp to string date and time

    What is timestamp in Python?

    In Python, the timestamp is defined as the UNIX timestamp. The Unix timestamp is the number of seconds from 1st January 1970 at UTC, this is the date when the Unix come into the business. The Unix timestamp is also known as the Epoch time, Posix time, seconds science Epoch or UNIX epoch time. The date and time 1 January 1970 00:00:00 UTC represents 0 seconds in Python timestamp.

    How to get the current Timestamp in Python?

    The current timestamp means the number of seconds from the Epoch time to the current date and time. And in Python, there are three inbuilt modules that can find the current timestamp

    1. datetime
    2. time
    3. calendar

    How to get the current timestamp using the datetime module in Python?

    The datetime module provides a timestamp() method that can convert a specific date-time object to the corresponding Unix timestamp which is seconds in float numbers. To get the current timestamp in Python using datetime, we first need to get the current date and time, then only we can convert it into the timestamp. Example

    from datetime import datetime
    
    #get the current date and time
    current_DateTime = datetime.now()
    
    #convert the current date and time into timestamp
    currentTimestamp = datetime.timestamp(current_DateTime)
    
    print("The current date and time is:", current_DateTime)
    print("The current TimeStamp is:",currentTimestamp)

    Output

    The current date and time is: 2022-01-11 11:26:33.106476
    The current TimeStamp is: 1641880593.106476

    In the above output, you can see that the timestamp function returns the time stamp in seconds.microseconds for more precision. We can also convert it into seconds by using the int() function.

    How to get the current timestamp using the time module in Python?

    To handle time data in Python, Python has a dedicated time module . The time module provides a time() method that returns the current timestamp in seconds from the epoch time January 1, 1970. The time() method returns the seconds timestamp in float point number as seconds.microseconds . Example

    import time
    
    #get the current timestamp
    currentTimestamp = time.time()
    
    print("The current TimeStamp is:",currentTimestamp)

    Output

    The current TimeStamp is: 1641884056.7937458

    How to get the current timestamp using the calendar module in Python?

    The calendar.timegm() method can convert the given current GMT time into a timestamp. To get the current GMT time we first use time.gmtime() method. Example

    import time
    import calendar
    
    #get the current time in GMT
    currentTime_GMT = time.gmtime()
    
    print("The current TimeStamp is:",calendar.timegm(currentTime_GMT))

    Output

    The current TimeStamp is: 1641884543

    How to convert the timestamp to DateTime in Python

    Let’s say you have given a timestamp and you need to convert it into a datetime object, how would you do that. The answer is simple, all you need is the datetime.fromtimestamp(timestamp) method and it will convert the given timestamp into date-time object. Syntax

    datetime.fromtimestamp(timestamp, tz=None)

    The fromtimestamp() method can accept two arguments

    • timestamp: the timestamp in seconds.microseconds that we want to convert into a date.
    • tz: An optional argument value which default value is None. tz argument represents the timezone and defaults None defines the local date-time timezone of the platform.

    Example

    from datetime import date, datetime
    
    #given timestamp
    timestamp = 1641885577.5728958
    
    #convert the timestamp to datetime object
    dateTime = datetime.fromtimestamp(timestamp)
    
    print(f"The timestamp {timestamp} in datetime is: ", dateTime)

    Output

    The timestamp 1641885577.5728958 in datetime is:  2022-01-11 12:49:37.572896

    Example: get the starting date of Epoch time using the fromtimestamp function Using the fromtimestamp() function we can also get the starting point of the timestamp. By passing the timestamp to 0 we can get the inception of the timestamp.

    from datetime import date, datetime
    
    #given timestamp
    timestamp = 0
    
    #convert the timestamp to datetime object
    dateTime = datetime.fromtimestamp(timestamp)
    
    print(f"The timestamp {timestamp} in datetime is: ", dateTime)

    Output

    The timestamp 0 in datetime is:  1970-01-01 05:30:00

    In the output, you can see the fromtimestamp() function return 1 January 1970 date and 05:30 time, however, the Epoch time is 1 January 1970 00:00:00. The difference of 5 hours and 30 minutes is showing became of timezone, the Epoch time is 1 January 1970 00:00:00 is UTC and my TimeZone is GMT+5:30. To get the date using the timestamp for UTC timezone we can use the datetime.utcfromtimestamp() method.

    from datetime import date, datetime
    
    #given timestamp
    timestamp = 0
    
    #convert the timestamp to datetime object (UTC)
    dateTime = datetime.utcfromtimestamp(timestamp)
    
    print(f"The timestamp {timestamp} in datetime is: ", dateTime)

    Output

    The timestamp 0 in datetime is:  1970-01-01 00:00:00

    Now the output shows the correct Epoch time which is 1 Jan 1970 00:00:00

    How to convert the timestamp into datetime string format?

    In the above section you learned how to convert the timestamp value to a datetime object, now let’s learn how to format that date-time object to string for a more readable date and time. To convert timestamp seconds to readable date and time string we first need to convert it into a datetime object. Then using the strftime() function we can format that datetime object to a more legible date and time string. Syntax

    datetime_obj.strftime('%d / %m / %Y %H:%M:%S')

    Example

    from datetime import date, datetime
    
    #given timestamp
    timestamp = 1641887531.007376
    
    #convert the timestamp to datetime object
    dateTime = datetime.fromtimestamp(timestamp)
    
    print(f"The timestamp {timestamp} in datetime is")
    print(dateTime.strftime("%d %b %Y, %I:%M %p"))

    Output

    11 Jan 2022, 01:22 PM

    In the above example the format specifiers

    • %d represent day 0 to 31
    • %b represent month Jan to Dec
    • %Y represent year
    • %I represent hours 01 to 12
    • %M represent Minutes
    • %p represents AM or PM (according to time)

    To know more about how to convert the datetime object to string click here .

    How to convert the timestamp seconds into milliseconds in python?

    The timestamp(), and time() return the timestamp in seconds.milliseconds, to convert those seconds into milliseconds we can multiply the returned timestamp by 1000. This is because there are 1000 milliseconds in 1 second. Example

    from datetime import  datetime
    
    #given date
    my_dob = datetime(1999,9,4)
    
    #convert the datetime into timestamp
    timestamp_Sec = datetime.timestamp(my_dob)
    
    #convert the timestamp into millisecond
    timestamp_milli = timestamp_Sec*1000
    
    #print the milliseconds timestamp
    print(timestamp_milli)

    Output

    936383400000.0

    Timestamp from datetime with a Different Timezone

    By default the datetime.now() and datetime.timestamp() uses the system default timezone. But we can also use the utcnow() and utcfromtimestamp() to get the time according to the UTC time standard. To get the different timestamps according to the timezone we can specify the time zone offset in the datetime object the timestamp() will return the correct timestamp according to that timezone. Example

    from datetime import  datetime
    
    #given date with 5:30 hours:minutes in UTC
    my_dob = datetime.strptime("04/Sep/1999:00:00:00 UTC +05:30", '%d/%b/%Y:%H:%M:%S %Z %z')
    
    #convert the datetime into timestamp
    timestamp_Sec = datetime.timestamp(my_dob)
    
    #print the timestamp
    print(timestamp_Sec)

    Output

    936383400.0
    Note: The UTC +05:30 shows the time zone of India and Sri Lanka.

    Conclusion

    In this Python tutorial, we learned what is timestamp in Python and how to work with it. The timestamp in Python represents the time in seconds from the Epoch time 1 January 1970 00:00:00 UTC. To get the current timestamp in Python we can use the datetime, time, or calendar module. The easiest way to get the current timestamp is the time() method of the time module. By default, the timestamp return by the time and datetime module uses the platform timezone for Epoch time.

    People are also reading:

    Leave a Comment on this Post

    0 Comments