python from datetime import datetime

BackEnd_Ash.log·2020년 6월 26일
0

https://stackoverflow.com/questions/9724906/python-date-of-the-previous-month

https://docs.python.org/ko/3/library/datetime.html

first import

import datetime
import dateutil.relativedelta

or you can write down

from datetime import datetime

anyway , you have to import datetime

second

print(now + dateutil.relativedelta.relativedelta(months=-1))
2020-05-26 12:35:28.132035

as another example

rint(now + dateutil.relativedelta.relativedelta(months=-8))
2019-10-26 12:35:28.132035

the only thing that changes months=
understand ?? :) awsome

third

if .. You want only year or month

In [10]: datetime.now().year
Out[10]: 2020

In [11]: datetime.now().month
Out[11]: 6

right!

if you want year , month ,day

from datetime import datetime
import dateutil.relativedelta

now = datetime.now()
year = datetime.now().year
month = datetime.now().month

day = str(now + dateutil.relativedelta.relativedelta(months=-1)).split()[0].replace("-","")
print(day) # 20200526

if you want year , month

from datetime import datetime
import dateutil.relativedelta

now = datetime.now()
year = datetime.now().year
month = datetime.now().month

day = str(now + dateutil.relativedelta.relativedelta(months=-1)).split()[0].replace("-", "")
print(day)  # 20200526
print(day[:6])  # 202005

I can't English well.
however, I think these things are piled up and become better later.

i love google translator
haha :)

datetime format

datetime.datetime(2020, 9, 10, 16, 47, 4, 330672, tzinfo=<UTC>)
In [8]: date.month
Out[8]: 9

In [9]: date.year
Out[9]: 2020

In [10]: date.year
Out[10]: 2020

In [11]: date.month
Out[11]: 9

In [12]: date.day
Out[12]: 10

formatting 규칙

4자리 year

now.strftime("%Y") # 2020

2자리 year

now.strftime("%y") # 20

2자리 month

now.strftime("%m") # 11

1자리 month

"{:d}".format(now.month) # 5

2자리 day

now.strftime("%d")

calendar

해당년도 달력 출력


import calendar

print(calendar.calendar(2020))
# 달력정보 모두 출력 

특정 연도의 월 달력 출력

import calendar

calendar.prmonth(2020,10)

특정 날짜의 요일 확인

import calendar

calendar.weekday(2020,7,13)
# 0 = 월요일
# 1 = 화요일
# 2 = 수요일
# 3 = 목요일
# 4 = 금요일
# 5 = 토요일
# 6 = 일요일
profile
꾸준함이란 ... ?

0개의 댓글