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

  • datetime can be found in the module datetime.
  • datetime is a python's representation of date and time in a single object.

how to create datetime object in python ?

from datetime import datetime
time_date = datetime(
year=2017, month=3, day=2, hour=14, minute=0, second=0, microsecond=0, tzinfo=None
)
print(type(time_date))
# Output: 'datetime.datetime'
print(time_date)
# Output: 2017-03-02 14:00:00

How to get date from datetime object ?

from datetime import datetime
time_date = datetime(
year=2017, month=3, day=2, hour=14, minute=0, second=0, microsecond=0, tzinfo=None
)
date = time_date.date()
print(type(date))
# Output: datetime.date
print(date)
# Output: 2017-03-02

How to get current datetime object in python ? 

from datetime import datetime
current_datetime = datetime.now()
print(current_datetime)
# Output: 2017-03-07 21:31:59.720195

How to get year, month, day, hour, minute, second in python?

from datetime import datetime
current_datetime = datetime.now()
year = current_datetime.year
month = current_datetime.month
day = current_datetime.day
hour = current_datetime.hour
minute = current_datetime.minute
second = current_datetime.second
microsecond = current_datetime.microsecond
print("year= %s, month=%s, day=%s, hour=%s, minute=%s, second=%s, microsecond=%s" % (year, month, day, hour, minute, second, microsecond))
# output: year= 2017, month=3, day=7, hour=21, minute=41, second=50, microsecond=781903

How to convert a datetime object to string with a specific format python(datetime.strftime)?

"2017-02-04 12:02:33"
from datetime import datetime
date = datetime(2017, 2, 4, 12, 2, 33)
s = datetime.strftime(d, "%Y-%m-%d %H:%M:%S")
print(s)
#Output: '2017-02-04 12:02:33'
"2017/02/04 12:02:33"
from datetime import datetime
date = datetime(2017, 2, 4, 12, 2, 33)
s = datetime.strftime(d, "%Y/%m/%d %H:%M:%S")
print(s)
#Output: 2017/02/04 12:02:33
"04/02/2017 12:02:33"
from datetime import datetime
date = datetime(2017, 2, 4, 12, 2, 33)
s = datetime.strftime(date, "%d/%m/%Y %H:%M:%S")
print(s)
# Output: '04/02/2017 12:02:33'
"04 February 2017 12:02:33 PM"
from datetime import datetime
date = datetime(2017, 2, 4, 12, 2, 33)
s = datetime.strftime(date, "%d %B %Y %H:%M:%S %p")
print(s)
# Output: 04 February 2017 12:02:33 PM
"Saturday February 2017 12:02:33 PM"
from datetime import datetime
date = datetime(2017, 2, 4, 12, 2, 33)
s = datetime.strftime(date, "%A %B %Y %H:%M:%S %p")
print(s)
# Output: Saturday February 2017 12:02:33 PM
"Sat February 2017 12:02:33 pm"
from datetime import datetime
date = datetime(2017, 2, 4, 12, 2, 33)
s = datetime.strftime(date, "%a %B %Y %H:%M:%S %P")
print(s)
# Output: Sat February 2017 12:02:33 pm

How to convert a string to datetime object python(datetime.strptime)? 

"2017-02-04 12:02:33"
from datetime import datetime
s = "2017-02-04 12:02:33"
d = datetime.strptime(s, "%Y-%m-%d %H:%M:%S")
print(d)
# Output: datetime.datetime(2017, 2, 4, 12, 2, 33)
"2017/02/04 12:02:33"
from datetime import datetime
s = "2017/02/04 12:02:33"
d = datetime.strptime(s, "%Y/%m/%d %H:%M:%S")
print(d)
# Output: datetime.datetime(2017, 2, 4, 12, 2, 33)
"04/02/2017 12:02:33"
from datetime import datetime
s = "04/02/2017 12:02:33"
d = datetime.strptime(s, "%d/%m/%Y %H:%M:%S")
print(d)
# Output: datetime.datetime(2017, 2, 4, 12, 2, 33)
"04 February 2017 12:02:33 PM"
from datetime import datetime
s = "04 February 2017 12:02:33 PM"
d = datetime.strptime(s, "%d %B %Y %H:%M:%S %p")
print(d)
# Output: datetime.datetime(2017, 2, 4, 12, 2, 33)
"04 February 2017 12:02:33 PM"
from datetime import datetime
s = "04 February 2017 12:02:33 PM"
d = datetime.strptime(s, "%d %B %Y %H:%M:%S %p")
print(d)
# Output: datetime.datetime(2017, 2, 4, 12, 2, 33)
"Saturday 04 February 2017 12:02:33 PM"
from datetime import datetime
s = "Saturday 04 February 2017 12:02:33 PM"
d = datetime.strptime(s, "%A %d %B %Y %H:%M:%S %p")
print(d)
# Output: datetime.datetime(2017, 2, 4, 12, 2, 33)
"Sat February 2017 12:02:33 pm"
from datetime import datetime
s = "Sat 04 February 2017 12:02:33 pm"
d = datetime.strptime(s, "%a %d %B %Y %H:%M:%S %p")
print(d)
# Output: datetime.datetime(2017, 2, 4, 12, 2, 33)
Reference: https://docs.python.org/2/library/datetime.html

Buy a product to Support me