Datetime Library

Datetime open library of Python,we can fetch datetime or any specific thing with the help of this module.

  1. Know current Date and time
  2. >>> import datetime
    >>> x=datetime.datetime.now()
    >>> print(x)
    2020-09-05 19:20:19.703489
  3. Print specific year month or day
  4. >>> import datetime
    >>> x=datetime.datetime.now()
    >>> x.year
    2020
    >>> x.month
    9
    >>> x.day
    5
    
  5. Creating Date objects
  6. Syntax : datetime(year,month,day)

    >>> import datetime
    >>> x=datetime.datetime(2009,12,22)
    >>> x
    datetime.datetime(2009, 12, 22, 0, 0)
    >>> 
    

The Strftime() method

Strftim() method is used for the make date object in a more readable string.

Directive Description
%a Return weekend,less details
%A Return weekend,full details
%w Return weekend,as number
%b Return Month name,sort version
%B Return Month name,full version
%d Return day of month
%m Return month(Number)
%y Return year,sort version
%Y Return year,full version
%H Return Hour from 0 to 24
%I Return Hour from 0 to 12
%p Return AM and PM
%M Return Minutes 0 to 59
%S Return Second 0 to 59
%f Return Microsecond 00000-999999
%z Return UTC offset
%Z Return time zone
%j Return Days of year
%U Return weekend number of year(Starting from monday)
%c Return local number datetime
%x Return local number date
%X Return local number time
  1. 1 Use of %a
  2. >>> import datetime
    >>> details=datetime.datetime.now()
    >>> print(details.strftime("%a"))
    Sat
    >>>
    
  3. Use of %A
  4. >>> import datetime
    >>> details=datetime.datetime.now()
    
    
    >>> print(details.strftime("%A"))
    Saturday
    >>>  
    
  5. Use of %w
  6. >>> import datetime
    >>> details=datetime.datetime.now()
    >>> print(details.strftime("%w"))
    6
    >>>
    
  7. use of %b
  8. >>> import datetime
    >>> details=datetime.datetime.now()
    >>> print(details.strftime("%b"))
    Sep
    >>>
    
  9. use of %B
  10. >>> import datetime
    >>> details=datetime.datetime.now()
    >>> print(details.strftime("%B"))
    September
    >>>
    
  11. use of %d
  12. >>> import datetime
    >>> details=datetime.datetime.now()
    >>> print(details.strftime("%d"))
    05
    >>> 
    
  13. use of %m
  14. >>> import datetime
    >>> details=datetime.datetime.now()
    >>> print(details.strftime("%m"))
    09
    >>>  
    
  15. use of %y
  16. >>> import datetime
    >>> details=datetime.datetime.now()
    >>> print(details.strftime("%y"))
    20
    >>>
    
  17. use of %Y
  18. >>> import datetime
    >>> details=datetime.datetime.now()
    >>> print(details.strftime("%Y"))
    2020
    >>> 
    
  19. use of %H
  20. >>> import datetime
    >>> details=datetime.datetime.now()
    >>> print(details.strftime("%H"))
    19
    >>>
    
  21. use of %I
  22. >>> import datetime
    >>> details=datetime.datetime.now()
    >>> print(details.strftime("%I"))
    07
    >>>
    
  23. use of %p
  24. >>> import datetime
    >>> details=datetime.datetime.now()
    >>> print(details.strftime("%y"))
    PM
    >>>
    
  25. use of %M
  26. >>> import datetime
    >>> details=datetime.datetime.now()
    >>> print(details.strftime("%M"))
    55
    
  27. use of %S
  28. >>> import datetime
    >>> details=datetime.datetime.now()
    >>> print(details.strftime("%S"))
    40
    >>>
    
  29. use of %f
  30. >>> import datetime
    >>> details=datetime.datetime.now()
    >>> print(details.strftime("%f"))
    971489
    >>>
    
  31. use of %j
  32. >>> import datetime
    >>> details=datetime.datetime.now()
    >>> print(details.strftime("%j"))
    249
    >>>
    
  33. use of %u
  34. >>> import datetime
    >>> details=datetime.datetime.now()
    >>> print(details.strftime("%u"))
    6
    
  35. use of %c
  36. >>> import datetime
    >>> details=datetime.datetime.now()
    >>> print(details.strftime("%c"))
    Sat Sep  5 20:03:44 2020
    
  37. use of %x
  38. >>> import datetime
    >>> details=datetime.datetime.now()
    >>> print(details.strftime("%x"))
    09/05/20
    
  39. use of %X
  40. >>> import datetime
    >>> details=datetime.datetime.now()
    >>> print(details.strftime("%X"))
    20:03:44
    >>>    
    
Subscribe Now