Posts

Showing posts from May, 2019

Playing with dates in python

Image
Date in python In this tutorial you will be able to. Get current date/time Get weeks first and last date based on current date/time Get months first and last date based on current date/time First we will find current date time Step 1: Import necessary libraries (for above all task this is necessary step) from datetime import datetime, timedelta from calendar import monthrange Step 2: Get current date/time and just print it today = datetime.now() print(today) Now we will find weeks first and last date based on current date/time start = datetime.now() - timedelta(days=datetime.now().weekday()) end = start + timedelta(days=6) print(start) print(end) Now we will find months first and last date based on current date/time start = datetime.now().replace(day=1) end = datetime.now().replace(day=monthrange(datetime.now().year, datetime.now().month)[1]) print(start) print(end) Complete code: from datetime import datetime, timedelta from cale