Thursday, 28 May 2015

Python datetime format

Problem domain:
  1. Create a variable called now and store the result of datetime.now() in it.
  2. Then, print the value of now.
Solve: 

from datetime import datetime

now = datetime.now()
print now

Output: 
current date

Printing current date year and month code

Format 1:
from datetime import datetime
now = datetime.now()
print now
print now.year
print now.month
print now.day


Format 2: from datetime import datetime
now = datetime.now()


print '%s/%s/%s' % (now.month, now.day, now.year, )


Format 3: from datetime import datetime
now = datetime.now()


print '%s:%s:%s' % (now.hour, now.minute, now.second )
Format 4: from datetime import datetime
now = datetime.now()


print '%s/%s/%s %s:%s:%s' % (now.month,now.day,now.year, now.hour, now.minute, now.second )

No comments:

Post a Comment