
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to convert Python DateTime string into integer milliseconds?
Python provides the time and datetime modules to convert a DateTime string into integer milliseconds. Key functions include time.time(), which gives the current time in seconds, and datetime.timestamp(), which converts datetime objects directly into seconds since the epoch. By multiplying these values by 1000, we can get the time in milliseconds.
Using time.time() Method
The time module in Python provides various methods and functions related to time. Here we use the time.time() method to get the current CPU time in seconds. The time is calculated since the epoch, which returns a floating-point number expressed in seconds. This value is multiplied by 1000 and rounded off with the round() function.
Epoch is the starting point of time and is platform-dependent. The epoch is January 1, 1970, 00:00:00 (UTC) on Windows and most Unix systems, and leap seconds are not included in the time in seconds since the epoch.
Example
In the following program, we use the time.gmtime(0) to get the epoch on a given platform and time.time() method to get the current time in seconds. Then we multiply by 1000 and we approximate the value by using the round() function.
import time obj = time.gmtime(0) epoch = time.asctime(obj) print("The epoch is:",epoch) curr_time = round(time.time()*1000) print("Milliseconds since epoch:",curr_time)
Following is the output of the above code:
The epoch is: Thu Jan 1 00:00:00 1970 Milliseconds since epoch: 1662373873162
Using datetime.utcnow() and Subtracting Epoch
Initially, we retrieve the current date by using the datetime.utc() method. Then we get the number of days since the epoch by subtracting the date (datetime(1970, 1, 1)) from the current date. For this date, we apply the .total_seconds() method to get the total number of seconds since the epoch. Finally, we round off the value to milliseconds by applying the round() function.
Example
In the following program, we retrieve the current UTC using the utcnow() method, subtract the epoch date datetime(1970, 1, 1) to get the time difference, then extract total seconds using the total_seconds() method (calculates the total number of seconds represented by a timedelta object), and convert it to milliseconds.
from datetime import datetime print("Current date in string format:",datetime.utcnow()) date= datetime.utcnow() - datetime(1970, 1, 1) print("Number of days since epoch:",date) seconds =(date.total_seconds()) milliseconds = round(seconds*1000) print("Milliseconds since epoch:",milliseconds)
Following is the output of the above code:
Current date in string format: 2022-09-05 10:31:52.853660 Number of days since epoch: 19240 days, 10:31:52.853671 Milliseconds since epoch: 1662373912854
Using timestamp() Function on a Specific Date
The timestamp() function from the datetime module is used to convert any datetime object to seconds since the epoch (floating-point number). This function helps convert a specific date rather than the current time.
Example
In the following program, we create a datetime object for January 1, 2018, using the datetime() method, then convert it into seconds since the epoch using the timestamp() method, and finally convert it into milliseconds (multiply by 1000) using the round() function.
import time from datetime import datetime dt = datetime(2018, 1, 1) milliseconds = int(round(dt.timestamp() * 1000)) print(milliseconds)
Following is the output of the above code:
1514745000000