Friday, July 12, 2019

Current date and time

Script

#!/usr/local/bin/python3.1

import datetime

now = datetime.datetime.now()

print
print ("Current time and date using str method of datetime object:")
print (str(now))

print
print ("Current time and date using instance attributes:")
print ("Current year: %d" %now.year)
print ("Current month: %d" %now.month)
print ("Current day: %d" %now.day)
print ("Current hour: %d" %now.hour)
print ("Current minute: %d" %now.minute)
print ("Current second: %d" %now.second)
print ("Current microsecond: %d" %now.microsecond)

print
print ("Current time and date using strftime:")
print (now.strftime("%H:%M-%d/%m/%Y"))



Execution

Current time and date using str method of datetime object:
2014-07-15 23:12:39.964582
Current time and date using instance attributes:
Current year: 2014
Current month: 7
Current day: 15
Current hour: 23
Current minute: 12
Current second: 39
Current microsecond: 964582
Current time and date using strftime:
23:12-15/07/2014



Current time and date using str method of datetime object:
2014-07-15 23:16:19.626171
Current time and date using instance attributes:
Current year: 2014
Current month: 7
Current day: 15
Current hour: 23
Current minute: 16
Current second: 19
Current microsecond: 626171
Current time and date using strftime:
23:16-15/07/2014


No comments:

Post a Comment