如何用Python获取过去几个月的日期信息?

2026-05-21 13:480阅读0评论SEO基础
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计225个文字,预计阅读时间需要1分钟。

如何用Python获取过去几个月的日期信息?

伪原创可改为模仿创作或改写版。以下是对以下内容的

原文开头内容(假设内容):> 伪原创是指通过对原创内容进行改写、调整,使其在保持原意的基础上呈现出新的面貌,以达到版权规避的目的。

改写后内容:> 模仿创作,即在保留原意的基础上,对内容进行改写和调整,使其焕然一新,避免版权冲突。

#!/usr/bin/env pythpn
import datetime
now = datetime.datetime.now()
date_now = now.date()


def get_date_month(mon=0):
last_m = (int(now.year) * 12 + int(now.month) - mon) % 12
if len(str(last_m)) == 1:
last_m = "%02d" % last_m
last_y = int((int(now.year) * 12 + int(now.month) - mon) / 12)
last_mon = '%s%s' % (last_y, last_m)

return last_mon

last_mon = get_date_month(4);
print(last_mon)

$python get_year_month.py
202203


如何用Python获取过去几个月的日期信息?

本文共计225个文字,预计阅读时间需要1分钟。

如何用Python获取过去几个月的日期信息?

伪原创可改为模仿创作或改写版。以下是对以下内容的

原文开头内容(假设内容):> 伪原创是指通过对原创内容进行改写、调整,使其在保持原意的基础上呈现出新的面貌,以达到版权规避的目的。

改写后内容:> 模仿创作,即在保留原意的基础上,对内容进行改写和调整,使其焕然一新,避免版权冲突。

#!/usr/bin/env pythpn
import datetime
now = datetime.datetime.now()
date_now = now.date()


def get_date_month(mon=0):
last_m = (int(now.year) * 12 + int(now.month) - mon) % 12
if len(str(last_m)) == 1:
last_m = "%02d" % last_m
last_y = int((int(now.year) * 12 + int(now.month) - mon) / 12)
last_mon = '%s%s' % (last_y, last_m)

return last_mon

last_mon = get_date_month(4);
print(last_mon)

$python get_year_month.py
202203


如何用Python获取过去几个月的日期信息?