Found 10476 Articles for Python

How can we do date and time math in Python?

Rajendra Dharmkar
Updated on 02-Nov-2023 02:07:05

4K+ Views

It is very easy to do date and time maths in Python using timedelta objects. Whenever you want to add or subtract to a date/time, use a datetime.datetime(), then add or subtract datetime.timedelta() instances. A timedelta object represents a duration, the difference between two dates or times. The timedelta constructor has the following function signature −datetime.timedelta([days[, seconds[, microseconds[, milliseconds[, minutes[, hours[, weeks]]]]]]])Note: All arguments are optional and default to 0. Arguments may be ints, longs, or floats, and may be positive or negative. You can read more about it here − https://docs.python.org/2/library/datetime.html#timedelta-objectsExampleAn example of using the timedelta objects and dates ... Read More

How to write a function to get the time spent in each function in Python?

Vikram Chiluka
Updated on 28-Aug-2025 11:26:06

3K+ Views

In Python, measuring the execution time of a function or block of code can be done using several functions and methods provided by built-in modules like time and datetime. The following are the methods to measure the execution time of Python functions. Using time.time() Method Using time.process_time() Function Using datetime.now() Function Using time.time() Method The time.time() method (returns the current UTC) can measure how long a function takes to run. It returns the current time in seconds since January 1, 1970 (known as the Unix epoch). To ... Read More

How to convert time seconds to h:m:s format in Python?

Pranav Indukuri
Updated on 28-Aug-2025 11:30:58

8K+ Views

In Python, while working on date and time, converting time seconds to h:m:s (Hours: Minutes: Seconds) format can be done using simple arithmetic operations and built-in modules like datetime and time. The following are several ways to convert time seconds to h:m:s format. Using arithmetic operations (Naive Method) Using the timedelta class of the datetime module Using time.strftime() with time.gmtime() Using arithmetic operations (Naive method) This is the simplest way to convert seconds into time in H:M:S format. We use basic mathematical operations like division and modulo ... Read More

How to convert a datetime string to millisecond UNIX time stamp?

Rajendra Dharmkar
Updated on 28-Aug-2025 11:52:42

1K+ Views

A millisecond UNIX timestamp is a number that shows how many milliseconds have elapsed since the beginning of the Unix epoch, which is January 1, 1970, at 00:00:00 Coordinated Universal Time (UTC) up to the current moment or specified date and time. Instead of counting time in days, hours, or minutes, it counts in milliseconds (1 second = 1000 milliseconds). In Python, the common way to convert a Datetime object to a milliseconds timestamp involves using the strptime() function (to parse a string into a Datetime object), then converting this datetime object into a UNIX timestamp by using the timestamp() method, ... Read More

Why do I get different timestamps in python on different machines?

Rajendra Dharmkar
Updated on 28-Aug-2025 11:55:32

266 Views

A timestamp represents a specific point in time as a numerical value. It typically measures the number of seconds that have elapsed since the Unix epoch, which is January 1, 1970, at 00:00:00 Coordinated Universal Time (UTC). The main reasons behind timestamp variations on different machines include differences in time zones, system clocks, locale settings, and the use of UTC and local time (such as IST, PST, or CET). In this article, we will explore timestamp differences across different machines by considering the following key factors. Different System Time Zones If each system were set to different time zones like ... Read More

How do I get an ISO 8601 date in string format in Python?

SaiKrishna Tavva
Updated on 16-Jun-2025 15:47:20

64K+ Views

The ISO 8601 standard defines an internationally recognised format for representing dates and times. ISO 8601 is a date and time format that helps remove different forms of the day, date, and time conventions worldwide. In this article, we will discuss several methods to get an ISO 8601 date in string format in Python. ISO 8601 Date Format In Python,  ISO 8601 date is represented as "YYYY-MM-DDTHH:MM:SS.mmmmmm" format. For example, August 25, 2023, is represented as 2023-08-25T14:35:45.123456. YYYY: Year (four digits) MM: Month (from 1-12) DD: Days (from ... Read More

How to convert Python DateTime string into integer milliseconds?

Pranav Indukuri
Updated on 28-Aug-2025 11:13:49

6K+ Views

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 ... Read More

How to compare Python DateTime with Javascript DateTime?

Rajendra Dharmkar
Updated on 28-Aug-2025 11:44:15

817 Views

Both Python and JavaScript have unique ways of representing date and time data. To compare Python datetime objects with JavaScript Date objects, we must ensure that both are converted to a common format, such as ISO 8601 strings or Unix timestamps (milliseconds since epoch). The following are two major differences between Python (datetime) and JavaScript (date) objects. Month Representation: JavaScript uses a 0-indexed month (0 for January, 11 for December), while Python uses a 1-indexed month (1 for January, 12 for December). Default Time Zone: Python defaults to UTC, while JavaScript defaults to ... Read More

How to compare calendar.timegm() vs. time.mktime() in Python?

SaiKrishna Tavva
Updated on 19-May-2025 17:37:30

1K+ Views

In Python, the mktime() function (from the time module) assumes that the passed tuple is in local time, while the calendar.timegm() (from the calendar module) assumes it's in GMT/UTC. Depending on the interpretation, the tuple represents a different time, so both functions return different values (seconds since the epoch are UTC-based). The difference between the values should be equal to the time zone offset of your local time zone. Understanding time.mktime() in Local Time Context The Python time.mktime() method converts the object form of local time into seconds since the epoch (January 1, 1970, 00:00:00 UTC). This method is the inverse function of localtime() and ... Read More

How do I convert a datetime to a UTC timestamp in Python?

SaiKrishna Tavva
Updated on 19-May-2025 17:56:03

36K+ Views

We can use the datetime module to convert a datetime to a UTC timestamp in Python. If we already have the datetime object in UTC, then the timestamp() function can be directly used to get a UTC timestamp. This function returns the time since epoch for that datetime object. If we have the datetime object in the local timezone, first replace the timezone info and then fetch the time. The following are the various methods to convert a datetime object into a UTC timestamp in Python. Using datetime.timestamp() with UTC-aware datetime Local ... Read More

Advertisements