How to compare time in different time zones in Python?

Rajendra Dharmkar
Published on 08-Jan-2018 15:02:10
When you have 2 different time objects with different timezones and you need to compare them, you first need to understand the difference between aware and naive objects. An aware datetime object is an object that holds the information about the timezone while naive object doesn't hold any timezone information.The ... Read More

How to do date validation in Python?

Rajendra Dharmkar
Published on 08-Jan-2018 14:59:47
The date validation you want to achieve in python will largely depend on the format of the date you have. The strptime function from the datetime library can be used to parse strings to dates/times. For example, import datetime date_string = '2017-12-31' date_format = '%Y-%m-%d' try:   date_obj = datetime.datetime.strptime(date_string, ... Read More

How to compare date strings in Python?

Rajendra Dharmkar
Published on 08-Jan-2018 14:57:10
Python date implementations support all the comparision operators. So, if you are using the datetime module to create and handle date objects, you can simply use the <, >, <=, >=, etc. operators on the dates. This makes it very easy to compare and check dates for validations, etc. For ... Read More

How to insert date object in MySQL using Python?

Rajendra Dharmkar
Published on 08-Jan-2018 14:55:23
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 in a string format before inserting it to your database. To do this, you can use the datetime ... Read More

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

Rajendra Dharmkar
Published on 08-Jan-2018 14:52:03
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 in a string format before inserting it to your database. To do this, you can use the datetime ... Read More

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

Rajendra Dharmkar
Published on 08-Jan-2018 14:48:54
You can very easily store and retrieve date into Sqlite3 database using the sqlite3 module. When inserting the date in the database, pass the date directly and Python handles it automatically. For example, import sqlite3 import datetime conn = sqlite3.connect(":memory:", detect_types=sqlite3.PARSE_DECLTYPES) conn.execute('''CREATE TABLE TEST (ID TEXT PRIMARY KEY NOT NULL, ... Read More

What are Python modules for date manipulation?

Rajendra Dharmkar
Published on 08-Jan-2018 14:44:32
There are many modules available in both the standard library and the PiPy repository for date manipulation. The most popular among these libraries are the following(in no particular order):- datetime (Standard library): The datetime module supplies classes for manipulating dates and times in both simple and complex ways. While date ... Read More

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

Rajendra Dharmkar
Published on 08-Jan-2018 14:41:18
It is not possible to get a suffix like st, nd, rd and th using the strftime function. The strftime function doesn't have a directive that supports this formatting. You can create your own function to figure out the suffix and add it to the formatting string you provide. For ... Read More

How to find only Monday's date with Python?

Rajendra Dharmkar
Published on 08-Jan-2018 14:38:59
You can find the next Monday's date easily with Python's datetime library and the timedelta object. You just need to take today's date. Then subtract the number of days which already passed this week (this gets you 'last' monday). Finally add one week to this date using a timedelta object ... Read More

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

Rajendra Dharmkar
Published on 08-Jan-2018 14:36:05
You can use the fromtimestamp function from the datetime module to get a date from a UNIX timestamp. This function takes the timestamp as input and returns the datetime object corresponding to the timestamp. For exmaple, import datetime timestamp = datetime.datetime.fromtimestamp(1500000000) print(timestamp.strftime('%Y-%m-%d %H:%M:%S'))This will give the output:2017-07-14 08:10:00 Read More
Advertisements