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))

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM