2012년 3월 10일 토요일

[Python] 오늘 날짜 출력 예제

import os
import datetime

# get today and try to return as string
today = datetime.datetime.now()

print "Current year: %d" % today.year
print "Current year: %d" % today.month
print "Current year: %d" % today.day

month_str = str(today.month)
day_str = str(today.day)

# months manipulation
if len(month_str) == 1:
month_str = "0" + month_str
else:
month_str = month_str

# day manipulation
if len(day_str) == 1:
day_str = "0" + day_str
else:
day_str = day_str

today_str = str(today.year) + month_str + day_str
tm_str = str(today.year) + month_str

print today_str

댓글 없음: