Python Timestamp With Examples

Posted in

Python Timestamp With Examples
vinaykhatri

Vinay Khatri
Last updated on December 24, 2024

    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 timestamp is in Python and how to work with them. 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 into a timestamp in Python?
    5. Format timestamp to string date and time.

    What is a 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 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.

    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. Only then 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 the 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 a 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 whose default value is None. the 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 a string for a more readable date and time.

    To convert timestamp seconds to readable date and time strings, we first need to convert them 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 represents days 0 to 31
    • %b represents the month Jan to Dec
    • %Y represents the year
    • %I represents hours 01 to 12
    • %M represents Minutes
    • %p represents AM or PM (according to time)

    To know more about how to convert the datetime object to a 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's 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 timestamp is 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 is returned by the time, and the datetime module uses the platform timezone for Epoch time.

    People are also reading:

    FAQs


    A timestamp is a set of characters and numbers that represent the date and time of the event that occurred.

    You will find three inbuilt modules in Python to get the current timestamp. They are: 1. datetime 2. time 3. calendar.

    You can convert the timestamp to DateTime in Python using the datetime.fromtimestamp(timestamp) method. You can refer to an example in the above article.

    The timestamp() and time() method returns timestamp in seconds.milliseconds. To convert those seconds, simply multiply by 1000.

    Leave a Comment on this Post

    0 Comments