Rajendra Dharmkar has Published 452 Articles

What is the difference between Python functions datetime.now() and datetime.today()?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 13-Jun-2020 06:04:46

1K+ Views

The function datetime.now() takes tzinfo as keyword argument but datetime.today() does not take any keyword arguments. Quoting the docs −datetime.now() returns the current local date and time. If optional argument tz is None or not specified, this is like today(), but, if possible, supplies more precision than can be gotten ... Read More

How to compare date strings in Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 13-Jun-2020 06:01:28

847 Views

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.Examplefrom datetime import datetime ... Read More

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

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 12-Jun-2020 14:00:29

1K+ Views

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.Exampleimport sqlite3 import datetime conn = sqlite3.connect(":memory:", detect_types=sqlite3.PARSE_DECLTYPES) conn.execute('''CREATE TABLE TEST (ID TEXT PRIMARY KEY NOT NULL, DATE DATE)''') # ... Read More

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

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 12-Jun-2020 13:42:44

1K+ Views

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.Examplefrom datetime ... Read More

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

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 12-Jun-2020 13:21:20

12K+ Views

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.Exampleimport datetime timestamp = datetime.datetime.fromtimestamp(1500000000) print(timestamp.strftime('%Y-%m-%d %H:%M:%S'))OutputThis will give the output −2017-07-14 08:10:00Read More

How to print date in a regular format in Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 12-Jun-2020 13:07:55

1K+ Views

If you print the dates directly using a print function, you'd get regular dates,  Exampleimport datetime today = datetime.date.today() print(today)OutputYou will get the output −2018-1-2which is exactly what you want. But when you append this to a list and then try to print it,   Exampleimport datetime my_list = [] today ... Read More

How to get last day of a month in Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 12-Jun-2020 12:18:44

228 Views

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 calender module has a method, monthrange(year, month) that returns the weekday of first day ... Read More

How to get first day of the week using Python?

Rajendra Dharmkar

Rajendra Dharmkar

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

1K+ 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 2Read More

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

Rajendra Dharmkar

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 ... Read More

How to get computer's UTC offset in Python?

Rajendra Dharmkar

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 ... Read More

Advertisements