Found 10419 Articles for Python

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

Pranav Indukuri
Updated on 25-Aug-2023 01:08:03

33K+ 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 number of days between two dates using python. datetime module Python has a built-in datetime module that assists us in resolving a number of date-related issues. We just input the two dates with the date type and subtract them to discover the difference between the two dates, which gives us the number of days between the two dates. Syntax The syntax of the date() method in datetime module is as follows. datetime.date(year, month, day) ... Read More

How to get the total number of leap days in the years within range in Python?

Pranav Indukuri
Updated on 08-Sep-2022 07:34:47

585 Views

In this article we will discuss how to find the number of leap years in a specified ranges of year in python. leapdays() The calendar module offers more helpful calendar-related tasks in addition to allowing calendars to be produced like programmes. The idealised calendar used by the functions and classes defined in the Calendar module is the present Gregorian calendar stretched indefinitely in both directions. For simple text calendars, the calendar module in Python provides the function calendar.leapdays(). Syntax the syntax of the leapdays() method is as follows. leapdays() The above function takes two parameters; they are − ... Read More

How to get first day of the week using Python?

Rajendra Dharmkar
Updated on 12-Jun-2020 11:54:08

2K+ Views

You can use the calender module to get the first day of the week using Python. The calender module has a special function, firstweekday(), that returns the current setting for the weekday to start each week.Exampleimport calendar print(calendar.firstweekday()) calendar.setfirstweekday(2) print(calendar.firstweekday())OutputThis will give the output −6 2

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

Rajendra Dharmkar
Updated on 12-Jun-2020 11:50:58

2K+ Views

The easiest way in Python date and time to handle timezones is to use the pytz and tzlocal modules. These libraries allows accurate and cross platform timezone calculations. pytz brings the Olson tz database into Python. It also solves the issue of ambiguous times at the end of daylight saving time, which you can read more about in the Python Library Reference (datetime.tzinfo).Before you use it you'll need to install it using −$ pip install pytz tzlocalExampleYou can use the pytz library as follows −from datetime import datetime from pytz import timezone from tzlocal import get_localzone format = "%Y-%m-%d %H:%M:%S ... Read More

How to get computer's UTC offset in Python?

Rajendra Dharmkar
Updated on 12-Jun-2020 11:47:41

2K+ Views

The computer's UTC offset is the timezone set on your computer. YOu can get this timezone information using the time module. time.timezone returns UTC offset in seconds.For exampleimport time print(-time.timezone) # India's timezone: +5:30OutputThis will give the output −19800You can also use other workarounds to get the timezone information. You can create datetime objects for UTC and local timezones and subtract them and finally get the difference to find the timezone.For exampleimport time from datetime import datetime ts = time.time() utc_offset = (datetime.fromtimestamp(ts) -               datetime.utcfromtimestamp(ts)).total_seconds()OutputThis will give the output −19800Read More

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 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

935 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

Advertisements