Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles on Trending Technologies
Technical articles with clear explanations and examples
How to get last day of a month in Python?
You can use the calendar module to find the weekday of first day of the month and number of days in month. Using this information you can easily get the last day of the month. The calendar module has a method, monthrange(year, month) that returns the weekday of first day of the month and number of days in month, for the specified year and month. Using calendar.monthrange() The monthrange() function returns a tuple containing the weekday of the first day and the total number of days in the month ? import calendar # Get weekday ...
Read MoreHow do I calculate number of days between two dates using Python?
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 MoreHow do I print a Python datetime in the local timezone?
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 MoreHow to get computer's UTC offset in Python?
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 MoreHow can I apply an offset on the current time in Python?
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 MoreHow to perform arithmetic operations on a date in Python?
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 MoreHow can we do date and time math in Python?
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 MoreHow to write a function to get the time spent in each function in Python?
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 MoreHow to convert time seconds to h:m:s format in Python?
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 MoreHow to convert a datetime string to millisecond UNIX time stamp?
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