首页 > 编程知识 正文

关于python第一百三十天的信息

时间:2023-12-04 11:50:00 阅读:311982 作者:MYGP

本文目录一览:

  • 1、python 获得一个月有多少天
  • 2、python给出年/月/日计算是此年的多少天?
  • 3、python 求日期
  • 4、python计算 从今天开始,100天后星期几

python 获得一个月有多少天

在python的datetime模块中没有一个月有多少天的方法,但是可以使用calendar模块获得。

如下代码:

import calendar

monthRange = calendar.monthrange(2013,6)

print monthRange

输出:

(5, 30)

输出的是一个元组,第一个元素是上一个月的最后一天为星期几(0-6),星期天为0;第二个元素是这个月的天数。

python给出年/月/日计算是此年的多少天?

import datetime

import calendar

year = int(input('请输度入4位数字的年份:'))  # 获取年份

month= int(input('请输入月份1到12之间:'))  # 获取月份

day= int(input('请输入日份1到31之间:'))  # 获取“日”

if(calendar.isleap(year)==True):

print('闰年')

else:

print('平年')

if(month == 1 or month == 3 or month == 5 or month == 7 or month == 8 or month == 10 or month == 12):

print('31天')

elif (month == 4 or month == 6 or month == 9 or month == 11 ):

print('30天')

elif month == 2 and ((year % 4==0 and year % 100!=0) or (year % 400==0)):

print('29天')

else:

print('28天')

targetDay = datetime.date(year, month, day)  # 将输入的日期专格式化成标准的日期

dayCount = targetDay - datetime.date(targetDay.year - 1, 12, 31)  # 减去上一属年最后一天

print('%s是%s年的第%s天。' % (targetDay, year, dayCount.days))

python 求日期

# -*- coding: cp936 -*-

#设置星期天的初始值为0

mondays=0

def getmonthdays(year):

    isleapyear=year%400==0 or (year%4==0 and (not year%100==0))

    if isleapyear:

        return [31,29,31,30,31,30,31,31,30,31,30,31]

    return [31,28,31,30,31,30,31,31,30,31,30,31]

#计算1899.12.31(这天是星期天)1901.1.1之间的天数

pastdays=1  #1899.12.31过一天是1900.1.1

monthdays=getmonthdays(1900)

for month in range (0,12):

    pastdays+=monthdays[month]

#计算1901.1.1到2000.12.31星期天的数字

for year in range(1901,2001):

    monthdays=getmonthdays(year)

    for month in range(0,12):

        if pastdays%7==0:

            mondays+=1

        pastdays+=monthdays[month]

print "1901年1月1月至2000年12月31日共有%d个星期天落在每月第一天"%mondays

python计算 从今天开始,100天后星期几

import time,datetime

def get_week_day(date):

week_day_dict = {

0 : '星期一',

1 : '星期二',

2 : '星期三',

3 : '星期四',

4 : '星期五',

5 : '星期六',

6 : '星期天',

}

day = date.weekday()

return week_day_dict[day]

days = 100

print(get_week_day(datetime.datetime.now() + datetime.timedelta( days )))

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。