Alphalearningschool
        

<<< Prev

Next >>>

Up
Python Date

In real world scenario, we use the python date and time.

For current date and time show, bellow example,

Ex.1
import datetime
print(datetime.datetime.now())

O/P
2019-01-03 12:03:23.766551

 Get current date, 

Ex.2
import datetime
print(datetime.date.today())


O/p
2019-01-03

 Now see what is inside the datetime module , for that we are using dir() function know this thing.

Ex.3
import datetime
print(dir(datetime))

O/P
['MAXYEAR', 'MINYEAR', '__builtins__', 
'__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', 
'__spec__', 'date', 'datetime', 'datetime_CAPI', 'time', 'timedelta', 'timezone', 'tzinfo']

 

Commonly used class In datetime module:

  1. date class
  2. time class
  3. datetime class
  4. timedelta class

datetime.date class

            Date object can represent a date (year , month , day).

Ex:
	import datetime
  print(datetime.date(2018,12,3))

O/P
2018-12-03

 

datetime.time class:

            Time object can represent time (hour , minute , seconds )

Ex:
		import datetime
       print(datetime.time(11,12,10))

		O/P
		11:12:10

 

datetime.datetime class:

            Datetime object can represent (year , month , day ,hour , minute , second , micro second).

Ex:
import datetime
print(datetime.datetime(2018,6,14,11,55,33,1234))
		
O/P
2018-06-14 11:55:33.001234

 

datetime.timedelta class:

            It will use for find the difference between two date and time.

Ex:
	import datetime
 	a=datetime.datetime(2018,6,14,11,55,33,1234)
 	b=datetime.datetime(2017,6,14,11,54,39,124)
 	c=a-b
 	print(c)
 	print("Difference ", type(c))

	O/P
	365 days, 0:00:54.001110
 	Difference  <class 'datetime.timedelta'>