Are you working on a Python program and now want to get the current timestamp in to your Python code? Then do not worry. Because we will now learn how to do just that!
Sounds good? Great! So let us start then!
Python comes with a variety of libraries to work with time. But among them, two of the libraries stand out for getting the current timestamp. These are the datetime library and the time library.
So we can use either of these Python libraries to get the current timestamp. Let us now learn how to use each of them. Alright?
Get Current Timestamp In Python Using Datetime Library
We can use Python’s datetime library to get the current timestamp. To do that, we will need to use it’s now() function. So the example code using datetime library to get the current timestamp looks something like this:
from datetime import datetime ts = datetime.now().strftime("%B %d, %Y") print("Current timestamp is: " + ts)
Here, the function datetime.now( ) will return the current timestamp. But the problem is, it is not in human readable format but rather in datetime object format. So we need to convert it from the datetime object to a human readable format. So to do that, we use another datetime function called strftime.
strftime – This function will convert the timestamp from datetime object to a human readable string.
So if you run the code above, you get an output that looks something like this:
'August 17, 2020'
As you can see, we now have our timestamp that is easy to read!
While this is one way for us to get our timestamp, we can use another library to do the same. And that is to use the time library. But how do we use it? Let us take a look at it in the next section.
Get Current Timestamp In Python Using Time Library
Just like the datetime library we saw earlier, we can use the time library to get our current timestamp. But how? To get current timestamp from the time library, we just need to call it’s time function.
Aah…not sure how to do that? Well, take a look at the code below:
import time secondsFromEpoch = time.time() print(secondsFromEpoch)
So what output do you get when you run the above code? Well, take a look at it for yourself:
Wait a second! What the hell is going on over here? What are these numbers? They do not look like a timestamp! Right?
Well, you are right in a way. It indeed does not look like a timestamp we normally recognize. But what if I told you that it’s value is actually correct?
Wait what? How is it right? Well, the reason is because it is showing the total number of seconds that has passed since the epoch time.
Wait a minute! Now what is epoch time?
What Is Epoch Time?
Epoch time is the number of seconds that have passed since the midnight of January 1, 1970 GMT.
So when we print the value of secondsFromEpoch, the value that we see is the number of seconds passed since the Epoch Time!
Okay, all that is well and good. But how do we make it more readable?
Well to make it more readable, we will use another helper function from the time module called the localtime( ) function.
So using this function, we can change the above code to print a more readable timestamp value. It looks like this:
import time secondsFromEpoch = time.time() timeObj = time.localtime(secondsFromEpoch) print("The current time is: %d/%d/%d %d:%d:%d" % ( timeObj.tm_mday, timeObj.tm_mon, timeObj.tm_year, timeObj.tm_hour, timeObj.tm_min, timeObj.tm_sec))
And this will give us the output:
The current time is: 17/8/2020 13:32:20
Now, this is more readable. Right?
So there you have it! This is how we can get the current timestamp in Python using the time module.
But are these the only two ways to get a timestamp? Well, no. As I said there are many libraries in Python we can use to get the current timestamp. But these are the two libraries that are easier to use.
Another library that comes to my mind is the calendar module. But this library works along with the time module. So what it does is, it uses the time tuple value given by time module’s gmtime( ) function.
It then converts this value to current time. Now the code looks something like this:
import calendar import time ts = calendar.timegm(time.gmtime()) print(ts)
So when you run this code, the output looks like this:
So there you have it. These are some of the ways we can get our current timestamp. But if you have any doubts around it, do let me know in the comments below. Alright? So until next time, take care!
Want to learn how to extract data from a website using Python? Check this article!