Programming Articles

Page 646 of 2547

How to compare time in different time zones in Python?

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

In this article, we will show you how to compare time in different timezones in Python using the below methods ? Comparing the given Timezone with the local TimeZone Comparing the Current Datetime of Two Timezones Comparing Two Times with different Timezone Method 1: Comparing Local Timezone with UTC This method compares a local timezone (CET) with UTC timezone to determine the time difference ? # importing datetime, pytz modules from datetime import datetime import pytz # Getting the local timezone localTimeZone = pytz.timezone('CET') # Getting the UTC timeZone utcTimeZone = ...

Read More

How to do date validation in Python?

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

In this article, we will show you how to do date validation in Python. Date validation ensures that date strings conform to expected formats and represent valid dates. Using datetime.strptime() Function The datetime.strptime() function parses a date string according to a specified format. It raises a ValueError if the date is invalid or doesn't match the format ? import datetime # Valid date string date_string = '2017-12-31' date_format = '%Y-%m-%d' try: # Parse the date string date_object = datetime.datetime.strptime(date_string, date_format) print(f"Valid date: ...

Read More

How to compare date strings in Python?

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

Python's datetime module makes comparing dates straightforward by supporting all comparison operators (, =, ==, !=). This is essential for date validations, sorting, and conditional logic in your applications. Basic Date Comparison Here's how to compare datetime objects using comparison operators ? from datetime import datetime, timedelta today = datetime.today() yesterday = today - timedelta(days=1) print("Today:", today.strftime("%Y-%m-%d")) print("Yesterday:", yesterday.strftime("%Y-%m-%d")) print() print("today < yesterday:", today < yesterday) print("today > yesterday:", today > yesterday) print("today == yesterday:", today == yesterday) Today: 2024-01-15 Yesterday: 2024-01-14 today < yesterday: False today > yesterday: True ...

Read More

How to store and retrieve a date into MySQL database using Python?

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

To insert a date in a MySQL database, you need to have a column of Type DATE or DATETIME in your table. Once you have that, you'll need to convert your date to a string format before inserting it into your database. You can use the datetime module's strftime() formatting function for this purpose. Setting up the Database Connection First, let's establish a connection to MySQL and create a sample table ? import mysql.connector from datetime import datetime # Create connection (adjust credentials as needed) connection = mysql.connector.connect( host='localhost', ...

Read More

How to store and retrieve date into Sqlite3 database using Python?

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

You can easily store and retrieve dates in SQLite3 database using Python's sqlite3 module. When inserting dates, pass the datetime.date object directly and Python handles the conversion automatically by using the detect_types=sqlite3.PARSE_DECLTYPES parameter. Setting Up the Database Connection First, create a connection with date parsing enabled and set up a table ? import sqlite3 import datetime # Enable automatic date parsing conn = sqlite3.connect(":memory:", detect_types=sqlite3.PARSE_DECLTYPES) conn.execute('''CREATE TABLE TEST (ID TEXT PRIMARY KEY NOT NULL, DATE DATE)''') conn.commit() print("Table created successfully") Table created successfully Storing Dates in the Database Insert ...

Read More

What are Python modules for date manipulation?

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 24-Mar-2026 325 Views

Python provides several powerful modules for date and time manipulation, each serving different purposes. Here are the most important modules available for working with dates and times in Python. Standard Library Modules datetime Module The datetime module is the primary built-in module for date and time manipulation. It provides classes for working with dates, times, and time intervals ? from datetime import datetime, date, time, timedelta # Current date and time now = datetime.now() print("Current datetime:", now) # Create specific date birthday = date(2024, 12, 25) print("Date:", birthday) # Date arithmetic future_date ...

Read More

How do I display the date, like "Aug 5th", using Python's strftime?

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

Python's strftime() function doesn't directly support ordinal suffixes like "st", "nd", "rd", and "th". You need to create a custom function to determine the appropriate suffix and combine it with strftime() formatting. Creating a Suffix Function First, let's create a helper function that determines the correct ordinal suffix for any day ? def get_suffix(day): if 4

Read More

How to find only Monday\'s date with Python?

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

In this article, we will show you how to find only Monday's date using Python. We find the last Monday, next Monday, nth Monday's dates using different methods ? Using timedelta() function Using relativedelta() function to get Last Monday Using relativedelta() function to get next Monday Using relativedelta() function to get next nth Monday Using timedelta() function to get previous nth Monday Using timedelta() for Next Monday The timedelta() function from Python's datetime library calculates differences between dates and can manipulate dates. It provides parameters for days, seconds, microseconds, milliseconds, minutes, hours, and weeks. ...

Read More

How to convert an integer into a date object in Python?

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

You can convert an integer representing a UNIX timestamp into a date object using Python's datetime module. The fromtimestamp() function takes the timestamp as input and returns the corresponding datetime object. Basic Conversion Using fromtimestamp() The simplest approach is to use datetime.fromtimestamp() ? import datetime timestamp = 1500000000 date_obj = datetime.datetime.fromtimestamp(timestamp) print(date_obj.strftime('%Y-%m-%d %H:%M:%S')) 2017-07-14 08:10:00 Converting to Date Only If you only need the date part without time, use the date() method ? import datetime timestamp = 1500000000 date_obj = datetime.datetime.fromtimestamp(timestamp) date_only = date_obj.date() print(date_only) print(type(date_only)) ...

Read More

How to get the Python date object for last Wednesday?

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

You can get the Python date object for last Wednesday using date arithmetic. The key is calculating how many days back Wednesday was from today, regardless of what day it currently is. Understanding the Logic Python's weekday() method returns 0 for Monday, 1 for Tuesday, 2 for Wednesday, and so on. To find last Wednesday, we subtract 2 (Wednesday's index) from today's weekday and use modulo 7 to handle week boundaries ? Example from datetime import date, timedelta today = date.today() print(f"Today is: {today} ({today.strftime('%A')})") # Calculate days back to last Wednesday offset ...

Read More
Showing 6451–6460 of 25,466 articles
« Prev 1 644 645 646 647 648 2547 Next »
Advertisements