Usage of "date" from datetime module with use cases - Python

  • Date is a reference to a particular day represented within a calendar system.
  • We use "date" class to represent a day in calendar in python
  • We import date from "datetime" module 

How to represent "date" in python ?

from datetime import date
d = date(year=2017, month=4, day=3)
print(d)
# Output: 2017-04-03

How to get current date in python ?

from datetime import date
d = date.today()
print(d)
# Output: we get today's date

How get year, month, day from month ?

from datetime import date
d = date.today()
year = d.year
month = d.month
day = d.day
print("year = %s, month = %s, day = %s" % (year, month, day))
# Output: year = 2017, month = 3, day = 12

How to replace year, month, day in date object and get new date object ?

from datetime import date
d = date(year=2017, month=4, day=3)
# replace year with 2015
old_date = d.replace(year=2015)
print(old_date)
# Output: 2015-04-03
print(d)
# Output: 2017-04-03
# in the same way we can replace month, day

How to convert/parse string to date object python ?

'2017-04-03' >>> "%Y-%m-%d"
from datetime import datetime
s = '2017-04-03'
date_object = datetime.strptime(s, "%Y-%m-%d").date()
print(date_object)
# Output: 2017-04-03
'Monday 03 April 2017' >>> "%A %d %B %Y"
from datetime import datetime
s = 'Monday 03 April 2017'
date_object = datetime.strptime(s, "%A %d %B %Y").date()
print(date_object)
# Output: 2017-04-03
'Mon 03 Apr 2017' >>> "%a %d %b %Y"
from datetime import datetime
s = 'Mon 03 Apr 2017'
date_object = datetime.strptime(s, "%a %d %b %Y").date()
print(date_object)
# Output: 2017-04-03

How to parse date object to string format python?

from datetime import datetime
d = date(2017, 04, 03)
date_string = d.strftime("%Y-%m-%d")
# output: '2017-04-03'
date_string = d.strftime("%A %d %B %Y")
# output: 'Monday 03 April 2017'
date_string = d.strftime("%a %d %b %Y")
# output: 'Mon 03 Apr 2017'

How to convert date object to "datetime" object ?

from datetime import datetime, date
d = date(2017, 04, 03)
datetime_object = datetime(d.year, d.month, d.day)
print(datetime_object)
# output: 2017-04-03 00:00:00
print(type(datetime_object))
# output: datetime.datetime

How to compare two date objects ?

from datetime import date
date1 = date(2015, 12, 8)
date2 = date(2017, 11, 9)
# check if date1 > date2
is_date1_greater = date1 > date2
print(is_date1_greater)
# output: False
is_date1_greater = date2 > date1
print(is_date1_greater)
# output: True

How to get number of days between two dates ?

from datetime import date
date1 = date(2015, 12, 8)
date2 = date(2017, 11, 9)
time_delta = date1 - date2
print("days = %s" % (time_delta.days))
# output: days = -702
time_delta = date2 - date1
print("days = %s" % (time_delta.days))
# output: days = 702

How to get week day from date object ?

Return the day of the week represented by the date. Monday == 0, Tuesday == 1, ... , Sunday == 6
from datetime import date
d = date(2015, 12, 8)
print(d.strftime("%A"))
# Output: Tuesday
day_of_week = d.weekday()
print(day_of_week)
# Output: 1

How to get ISO week day from date object ?

Return the day of the week represented by the date. Monday == 1, Tuesday == 2, ... , Sunday == 7
from datetime import date
d = date(2015, 12, 8)
print(d.strftime("%A"))
# Output: Tuesday
day_of_week = d.isoweekday()
print(day_of_week)
# Output: 2
Reference:https://docs.python.org/2/library/datetime.html

Buy a product to Support me