Python gets the date of the last day of the previous month

大家讲道理
Release: 2016-11-09 10:49:35
Original
3368 people have browsed it

[Python] Code

from datetime import datetime
from datetime import timedelta
import calendar
def getLastDayOfLastMonth():
    d = datetime.now()
    c = calendar.Calendar()
     
    year = d.year
    month = d.month
     
    if month == 1 :
        month = 12
        year -= 1
    else :
        month -= 1
    days = calendar.monthrange(year, month)[1]   
    return (datetime(year,month,1)+timedelta(days= days)).strftime('%Y-%m-%d %X')
Copy after login


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template