Python Date and time

datetime is module are available in python programming. this module are provides lot of methods and functionality to operates data time operation. when we importing this modules then we capable to utilize those functionality. before start learn of this module methods first view its available function.

Help on module datetime:

NAME
    datetime - Fast implementation of the datetime type.

MODULE REFERENCE
    https://docs.python.org/3.5/library/datetime.html
    
    The following documentation is automatically generated from the Python
    source files.  It may be incomplete, incorrect or include features that
    are considered implementation detail and may vary between Python
    implementations.  When in doubt, consult the module reference at the
    location listed above.

CLASSES
    builtins.object
        date
            datetime
        time
        timedelta
        tzinfo
            timezone
    
    class date(builtins.object)
     |  date(year, month, day) --> date object
     |  
     |  Methods defined here:
     |  
     |  __add__(self, value, /)
     |      Return self+value.
     |  
     |  __eq__(self, value, /)
     |      Return self==value.
     |  
     |  __format__(...)
     |      Formats self with strftime.
     |  
     |  __ge__(self, value, /)
     |      Return self>=value.
     |  
     |  __getattribute__(self, name, /)
     |      Return getattr(self, name).
        |  __gt__(self, value, /)
     |      Return self>value.
     |  
     |  __hash__(self, /)
     |      Return hash(self).
     |  
     |  __le__(self, value, /)
     |      Return self<=value.
     |  
     |  __lt__(self, value, /)
     |      Return self<value.
     |  
     |  __ne__(self, value, /)
     |      Return self!=value.
     |  
     |  __new__(*args, **kwargs) from builtins.type
     |      Create and return a new object.  See help(type) for accurate signature.
     |  
     |  __radd__(self, value, /)
     |      Return value+self.
     |  
     |  __reduce__(...)
     |      __reduce__() -> (cls, state)
     |  
     |  __repr__(self, /)
     |      Return repr(self).
     |  
     |  __rsub__(self, value, /)
     |      Return value-self.
     |  
     |  __str__(self, /)
     |      Return str(self).
     |  
     |  __sub__(self, value, /)
     |      Return self-value.
     |  
     |  ctime(...)
     |      Return ctime() style string.
     |  
#so on

this module are provide various of functionality to use datetime format.

Example Datetime format

there are various way to display a format of date. for example display current date.

import datetime
#display current date and time
print(datetime.datetime.today())

today() function is return current date of our system.

Output
2019-02-25 10:42:04.040046

now() function is also similar to today() function.

import datetime
#get current date and time
date=datetime.datetime.now()

#display date
print(date) 
Output
2019-02-25 10:55:43.679518
import datetime
date=datetime.datetime.today()

print("Date is",date)
#Display Date Formats
print("year",date.year) #display year
print("month",date.month) #display month
print("day",date.day) #display month date day
print("minute",date.minute) #display minute
print("second",date.second) #display second
Output
Date is 2019-02-25 13:02:26.958789
year 2019
month 2
day 25
minute 2
second 26

Some format example

import datetime
date=datetime.datetime.today()
#Display Date Formats
result="{:%Y-%m-%d %H:%M:%S}".format(date)
print("Timestamp ",result) #Display timestamp

result="{:%Y-%m-%d}".format(date)
print("Date ",result) #Display date

result="{:%H:%M:%S}".format(date)
print("Time ",result) #Time

result="{:%H}".format(date)
print("Hour ",result) #hour

result="{:%M}".format(date)
print("Minute ",result) #minute

result="{:%S}".format(date)
print("Second ",result) #second
Timestamp  2019-02-25 11:52:41
Date  2019-02-25
Time  11:52:41
Hour  11
Minute  52
Second  41

another example

import datetime
date=datetime.datetime.today()
#Display Date Formats
result="{:%Y-%m-%d %H:%M:%S}".format(date)
print("Timestamp ",result) #Display timestamp

result="{:%b-%m-%d}".format(date)
print(result) #Display date

result="{:%B-%m-%d}".format(date)
print(result) #Display date

result="{:%B-%m-%d %a}".format(date)
print(result) #Display date



result="{:%X}".format(date)
print("Time ",result) #Display date

result="{:%x}".format(date)
print(result) #Display date

result="{:%x %X}".format(date)
print(result) #Display date

result="{:%x %X %p}".format(date)
print(result) #Display date

result="{:%b %d %Y}".format(date)
print(result) #Display date

result="{:%a %b %d %Y}".format(date)
print(result) #Display date
Output
Timestamp  2019-02-25 12:36:35
Feb-02-25
February-02-25
February-02-25 Mon
Time  12:36:35
02/25/19
02/25/19 12:36:35
02/25/19 12:36:35 PM
Feb 25 2019
Mon Feb 25 2019

Other datetime module function

ctime() this function are accept object of datetime.

import datetime
#get current date and time
date=datetime.datetime.today()

#display formatted date
print(datetime.datetime.ctime(date)) 
Output
Mon Feb 25 10:48:53 2019

strftime(): this function accept one parameter which is format of date.

import datetime
date=datetime.datetime.today()
#Display Date Formats

print(date.strftime("%x")) 
print(date.strftime("%d %m %y")) 
print(date.strftime("%b"))
print(date.strftime("%B"))
print(date.strftime("%X %p"))
Output
02/25/19
25 02 19
Feb
February
12:44:41 PM

time() this function are accept on datetime object and return time format.

import datetime
date=datetime.datetime.today()
print("Date & Time",date)
print("Time",datetime.datetime.time(date))
Output
Date & Time 2019-02-25 13:11:16.836158
Time 13:11:16.836158


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved