1. 程式人生 > >在python中獲取當前日期字串(格式:X年X月X日)

在python中獲取當前日期字串(格式:X年X月X日)

編碼環境:

  gvim 7.4

執行環境:

  win7-32(en)powershell

  python 2.7.11

程式碼:

# -*- coding: utf-8 -*-
import sys  
reload(sys)  
sys.setdefaultencoding('utf8')

import time, datetime

def get_today1():
    day = datetime.date.today()    
    strtmp = [str(day.year), '年', str(day.month), '月', str(day.day), '日']
    strtoday = ''.join(strtmp)

    return strtoday.encode('936')


def get_today2():
    day = datetime.date.today()
    strtoday = '%d年%d月%d日' %(day.year, day.month, day.day)

    return strtoday.encode('936')


def get_today3():
    cur_time = time.time()
    strtoday = time.strftime('%Y年%m月%d日',time.localtime(cur_time))

    return strtoday.encode('936')


str = get_today1()
print str, str[6:], str[9:]
str = get_today2()
print str
str = get_today3()
print str, str[6:], str[10:]
執行結果:

2016年3月22日  3月22日  22日
2016年3月22日
2016年03月22日  03月22日  22日