大曦百科全书(Daxipedia)
大黄 & 小曦 grow up and grow old together_ golden wedding since 2010

Jul 11, 2013

Python_Learning: A example to understand the Object-oriented Programming Language

# import the system time
from datetime import datetime
now = datetime.now()
print now

# Then the variable "now" has all attributes and functions of datetime data type!! you can easily get any of them, using dot notation. For example:
    print now.year
    print now.month
    print now.day
    print now.hour
    print now.minute
    print now.second

The result:








As a matter of fact, "now.year" equals to "datetime.now().year"

Notably, "print now.year" the result is "2013", and it is a numeric data type. Use str() to convert it to string data type.

#To display the system time in a pretty way
print str(now.month) + "/" + str(now.day) + "/" + str(now.year)  
print str(now.hour) + ":"  + str(now.minute) + ":" + str(now.second)

The result:
07/11/2013
16:51:57

No comments:

Post a Comment

Blog Archive