Show Current Time

  1. Consider the following code, which import the datetime module to get the year,month,day,hour,minute,second of the current time.
    
    	# Demonstration of the datetime module
    	import datetime
    	d = datetime.datetime.today()
    	print(d.year, d.month, d.day)
    	print(d.hour, d.minute, d.second)
    
    
  2. Suppose we save the code in a file "date.py", then you may run the program with the command "python date.py".
  3. As a matter of fact, we can change the Python script to be "executable" by the following steps:
    1. Insert the following code at the beginning of "date.py".
      
      	#!/usr/local/bin/python
      
      
    2. The program would become
      
      	#!/usr/local/bin/python
      	# Demonstration of the datetime module
      	import datetime
      	d = datetime.datetime.today()
      	print(d.year, d.month, d.day)
      	print(d.hour, d.minute, d.second)
      
      
    3. Save the file.
    4. At the Unix prompt, type the command "chmod +x date.py".
    5. Now you may type the command "date.py" to run the program and see the result.
  4. Modify your previous exercise about argv, so that when argc == 1, the program will automatically take the current year and month, instead of asking the user to input YYYY.MM.