Found 10449 Articles for Python

How can I apply an offset on the current time in Python?

Rajendra Dharmkar
Updated on 04-Nov-2023 01:13:51

4K+ Views

Whenever you want to add or subtract(apply an offset) 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-objects Example An example of using the timedelta objects and dates − import datetime old_time = datetime.datetime.now() print(old_time) ... Read More

How to perform arithmetic operations on a date in Python?

Vikram Chiluka
Updated on 02-Nov-2023 02:03:02

8K+ Views

In this article, we will show you how to perform arithmetic operations on a date in Python. Now we see 5 examples for this task− Adding days to a given Date Subtracting days to a given Date Adding Days and Hours to the given date Subtracting months from a current date Adding years for a given date Example 1: Adding days to a given Date Algorithm (Steps) Following are the Algorithm/steps to be followed to perform the desired task − Use the import keyword, to import the datetime module. Enter the date and create a variable to ... Read More

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 22-Sep-2022 11:58:27

3K+ Views

In this article, we will show you how to write a function to get the time spent in each function using python. Now we see 4 methods to accomplish this task− Now we see 2 methods to accomplish this task− Using time.clock() function Using time.time() function Using time.process_time() function Using datetime.now() function Method 1: Using time.clock() Python's Time module provides a variety of time−related functions. The method time.clock() returns the current processor time as a floating point number expressed in seconds on Unix. The precision depends on that of the C function of the same name, but in ... Read More

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

Pranav Indukuri
Updated on 08-Sep-2022 07:22:02

7K+ Views

In this article, we will discuss various ways to convert time in seconds to H M S format in python. H M S format means Hours: Minutes: Seconds. Using arithmetic operations (Naïve method) In this method, we use mathematic calculations to convert time in seconds to h m s format. Here we take the input from the user in seconds and convert it into the required format. Example In this example, we convert a time in seconds to h m s format. seconds = int(input("Enter the number of seconds:")) seconds = seconds % (24 * 3600) hour = seconds // ... Read More

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

Rajendra Dharmkar
Updated on 19-Feb-2020 08:08:58

1K+ Views

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.  exampleimport time milliseconds = int(round(time.time() * 1000)) print(milliseconds)OutputThis will give the output −1514825676008If 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. exampleimport time from datetime import datetime dt = datetime(2018, 1, 1) milliseconds = int(round(dt.timestamp() * 1000)) print(milliseconds)OutputThis ... 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

63K+ 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 compare calendar.timegm() vs. time.mktime() in Python?

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

932 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

How to convert timestamp string to datetime object in Python?

SaiKrishna Tavva
Updated on 15-May-2025 18:49:39

23K+ Views

In many real-world applications, timestamps are used to represent dates and times, but they are not human-readable. To make them understandable or use them in various datetime manipulations, it’s essential to convert them into Python’s datetime object. Python’s datetime module provides multiple functions to convert timestamps to datetime objects. Below are the various methods to accomplish this task - Using datetime.fromtimestamp() Function Using datetime.fromtimestamp() & strftime() Using datetime.strptime() Function Parsing Mixed Text Using strptime() Function Using datetime.fromtimestamp() Function To obtain a date ... Read More

Advertisements