
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How to convert a datetime string to millisecond UNIX time stamp?
You can get the current time in milliseconds in Python using the time module. You can get the time in seconds using time.time function(as a floating point value). To convert it to milliseconds, you need to multiply it with 1000 and round it off.
example
import time milliseconds = int(round(time.time() * 1000)) print(milliseconds)
Output
This will give the output −
1514825676008
If you want to convert a datetime object to milliseconds timestamp, you can use the timestamp function then apply the same math as above to get the milliseconds time.
example
import time from datetime import datetime dt = datetime(2018, 1, 1) milliseconds = int(round(dt.timestamp() * 1000)) print(milliseconds)
Output
This will give the output −
1514745000000
- Related Articles
- How to convert MySQL datetime to Unix timestamp?
- How to convert a Unix timestamp to time in JavaScript?
- Java Program to convert millisecond to readable string
- How to convert JS date time to MySQL datetime?
- How to convert unix timestamp string to readable date in Python?
- How to convert timestamp string to datetime object in Python?
- How to Convert Time String to Time in Excel?
- How to convert JavaScript datetime to MySQL datetime?
- How to add time in minutes in datetime string PHP?
- How to convert string to 24-hour datetime format in MySQL?
- How to convert Python date string mm/dd/yyyy to datetime?
- How to convert string to time in MySQL?
- Convert dd/mm/yyyy string to Unix timestamp in MySQL?
- How to convert Python date to Unix timestamp?
- Convert string to DateTime and vice-versa in Python

Advertisements