Articles on Trending Technologies

Technical articles with clear explanations and examples

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

How to convert Python date in JSON format?

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

There is no standard JSON format for dates. However, the ISO 8601 format is widely accepted as it's human readable, sorts correctly, includes fractional seconds, and is compatible with most programming languages. Python provides several methods to convert dates to JSON-compatible ISO format. Using isoformat() Method The isoformat() method is the simplest way to convert a Python datetime object to ISO 8601 format ? from datetime import datetime my_date = datetime.now() print(my_date.isoformat()) 2018-01-02T22:08:12.510696 Using strftime() Method For more control over the output format, you can use the strftime() function ...

Read More

How to sort a Python date string list?

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

Python provides several methods to sort a list of date strings. We'll explore three approaches: using sort() with lambda functions, using sort() with custom functions, and using sorted() function. Using sort() with Lambda Functions The datetime.strptime() function converts date strings into datetime objects for proper chronological sorting: # importing datetime from datetime import datetime # input list of date strings date_list = ['06-2014', '08-2020', '4-2003', '04-2005', '10-2002', '12-2021'] # sorting the input list by formatting each date using the strptime() function date_list.sort(key=lambda date: datetime.strptime(date, "%m-%Y")) # Printing the input list after sorting print("The ...

Read More

How to print date in a regular format in Python?

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

When working with dates in Python, you might encounter different output formats depending on how you print them. Understanding the difference between string and object representations is crucial for proper date formatting. Direct Date Printing When you print a date object directly, Python automatically converts it to a readable string format ? import datetime today = datetime.date.today() print(today) 2024-01-02 Date Objects in Lists However, when you add date objects to a list and print the list, you see the object representation instead of the formatted string ? import ...

Read More
Showing 7491–7500 of 61,303 articles
« Prev 1 748 749 750 751 752 6131 Next »
Advertisements