Python Articles

Page 755 of 855

How do I calculate number of days between two dates using Python?

Pranav Indukuri
Pranav Indukuri
Updated on 24-Mar-2026 34K+ Views

In this article we will discuss how to find the number of days between two dates using Python. We use the datetime module to calculate the difference between dates, which returns a timedelta object containing the number of days. Using datetime.date() Python's built-in datetime module provides the date class for handling dates. To find the difference between two dates, we create date objects and subtract them ? Syntax datetime.date(year, month, day) Parameters year − Integer representing the year (MINYEAR ≤ year ≤ MAXYEAR) month − Integer representing the month (1 ≤ ...

Read More

How do I print a Python datetime in the local timezone?

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 24-Mar-2026 2K+ Views

The easiest way to print a Python datetime in the local timezone is to use the pytz and tzlocal modules. These libraries provide accurate and cross-platform timezone calculations. pytz brings the Olson tz database into Python and solves the issue of ambiguous times at the end of daylight saving time. Before you use it you'll need to install it using − $ pip install pytz tzlocal Using pytz and tzlocal You can use the pytz library to convert UTC time to local timezone − from datetime import datetime from pytz import timezone ...

Read More

How to get computer's UTC offset in Python?

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 24-Mar-2026 3K+ Views

The computer's UTC offset is the timezone set on your computer. You can get this timezone information using Python's time module or datetime module. The UTC offset represents the time difference from Coordinated Universal Time (UTC) in seconds. Using time.timezone The time.timezone attribute returns the UTC offset in seconds. Note that it returns a negative value, so we negate it to get the actual offset ? import time # Get UTC offset in seconds (negated because time.timezone is negative) utc_offset = -time.timezone print("UTC offset in seconds:", utc_offset) # Convert to hours hours = utc_offset ...

Read More

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

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 24-Mar-2026 5K+ 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. Syntax 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. Adding Time Offset Here's how to add time to the current datetime ? import datetime ...

Read More

How to perform arithmetic operations on a date in Python?

Vikram Chiluka
Vikram Chiluka
Updated on 24-Mar-2026 8K+ Views

Performing arithmetic operations on dates allows us to calculate differences between dates, add or subtract time intervals, or compare one date with another using the datetime module in Python. This article will discuss how to perform several arithmetic operations using the datetime module in Python. Adding and Subtracting Days Using timedelta In the Python datetime module, timedelta is a class that represents the difference or duration between two dates or times. We can use timedelta objects to perform date arithmetic, such as adding or subtracting a certain number of days, weeks, hours, minutes, etc. To add ...

Read More

How can we do date and time math in Python?

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 24-Mar-2026 4K+ Views

It is very easy to do date and time math 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. Syntax 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 integers, longs, or floats, and may be positive or negative. Adding and Subtracting Time An example ...

Read More

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

Vikram Chiluka
Vikram Chiluka
Updated on 24-Mar-2026 3K+ Views

In Python, measuring the execution time of functions is essential for performance analysis and optimization. Python provides several built-in modules like time and datetime to measure function execution time effectively. Using time.time() Method Using time.process_time() Function Using datetime.now() Function Creating a Timer Decorator Function Using time.time() Method The time.time() method returns the current time in seconds since January 1, 1970 (Unix epoch). This method measures wall-clock time, including time spent waiting for I/O operations or system delays. To ...

Read More

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

Pranav Indukuri
Pranav Indukuri
Updated on 24-Mar-2026 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 ...

Read More

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

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 24-Mar-2026 2K+ 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 string 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 ...

Read More

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

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 24-Mar-2026 379 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 ...

Read More
Showing 7541–7550 of 8,546 articles
« Prev 1 753 754 755 756 757 855 Next »
Advertisements