python获取每月第一天、上月第一天

#-*- coding: utf-8 -*- 
import time
cur_year = time.localtime()[0]
cur_month = time.localtime()[1]

if cur_month < 10:
    cur_month = '0'+str(cur_month)
else:
    cur_month = str(cur_month)
    
cur_month_1st= "%s-%s-%s" % (cur_year, cur_month, '01')
cur_month_id = "%s%s" %(cur_year,cur_month)

pre_month = int(cur_month) - 1

if int(cur_month) == 1:
    pre_month = 12
    cur_year = cur_year -1

if pre_month <10:
    pre_month = '0'+str(pre_month)
else:
    pre_month = str(pre_month)


pre_month_1st = "%s-%s-%s" % (cur_year, pre_month, '01')
pre_month_id = "%s%s" %(cur_year,pre_month)

print("pre_month_id:%s,pre_month_1st:%s,cur_month_id:%s,cur_month_1st:%s" %(pre_month_id,pre_month_1st,cur_month_id,cur_month_1st))

 

posted @ 2019-09-16 16:08  bugbeta  阅读(5568)  评论(0编辑  收藏  举报