A. python判断年月日的问题
对的,你把下标用错了而已,应该为days[1] += 1 ,才是润二月的29天。
year=int(input('year: '))
month=int(input('month: '))
day=int(input('day: '))
days=[31,28,31,30,31,30,31,31,30,31,30,31]
ifyear%400==0or(year%4==0andyear%100!=0):
days[1]+=1
now=sum(days[0:month-1])+day
print(now)
B. Python django 做时间判断
djaogo 时间判断表达
时间
1、gt:大于某个时间
now = datetime.datetime.now()
前一天
start = now – datetime.timedelta(hours=23, minutes=59, seconds=59)
a=yourobject.objects .filter(youdatetimcolumn__gt=start)
2、gte:大于等于某个时间:
a=yourobject.objects .filter(youdatetimcolumn__gte=start)
3、lt:小于
a=yourobject.objects .filter(youdatetimcolumn__lt=start)
4、lte:小于等于
a=yourobject.objects .filter(youdatetimcolumn__lte=start)
5、range:查询时间段
start_date = datetime.date(2005, 1, 1)
end_date = datetime.date(2005, 3, 31)
Entry.objects.filter(pub_date__range=(start_date, end_date))
6、year:查询某年
Entry.objects.filter(pub_date__year=2005)
7、month:查询某月
Entry.objects.filter(pub_date__month=12)
8、day:某天
Entry.objects.filter(pub_date__day=3)
9、week_day:星期几
Entry.objects.filter(pub_date__week_day=2)
10、获取今天的日期,日期格式为yyyy-MM-dd
from django.utils.timezone import now, timedelta
date = now().date() + timedelta(days=-1) #昨天
date = now().date() + timedelta(days=0) #今天
date = now().date() + timedelta(days=1) #明天
C. python 程序语言 输入三个数(年 月 日)判断是否为合法日期 包括闰年
importdatetime
date_=raw_input('inputthreenumberlike2011231:')
try:
year,month,day=date_.split()
printyear,month,day
printdatetime.date(int(year),int(month),int(day))
exceptException,e:
printe
D. python判断给定的字符串是否是有效日期的方法
不太清楚你说的有效日期具体指什么,如果是普通的标准时间格式的话,python有一个模块有日期格式相关的解析
使用的模块:
fromdateutilimportparser
相关关键解析语句:
str(parser.parse(value))
解析效果:
解析前:
1995 03 21 12 12 12
1995-03-21 12:12:12
1995:03:21 12:12:12
1995/03/21 12:12:12
19950321121212
解析后:
统一变为标准时间格式:1995-03-21 12:12:12
因此利用这个模块可以匹配到有效日期
希望我的回答可以帮到你:-)
E. 用python判断是否为一个合法日期
import datetime
date = "03/31/2013/"
def check(date):
date = date.split('/')
m = int(date[0])
d = int(date[1])
y = int(date[2])
try:
datetime.date(y,m,d)
return true
except:
return false
F. 需要一个判断日期是否合法的python程序,包括闰年的判别,谢谢
这是一个quick and dirty的方法
datetime.date:表示日期的类。常用的属性有year, month, day;
try:
datatime.date( y,m,d )
return true
except:
return false
易水萧萧好无耻啊。好歹提一下引用了我的代码
G. 关于python中的日期推算
#coding=utf-8
'''
Createdon2014-12-29
@author:NeoWu
'''
c=0
importcalendar
foryearinxrange(1901,1902):
formonthinxrange(1,13):
'''
1.加了些打印帮助你理解
2.calendar.monthcalendar(year,month)这个返回的是传入的那一年的某个月的【星期列表】:
[[0,0,0,0,0,0,1],
[2,3,4,5,6,7,8],
[9,10,11,12,13,14,15],
[16,17,18,19,20,21,22],
[23,24,25,26,27,28,29],
[30,31,0,0,0,0,0]]
3.calendar.monthcalendar(year,month)[0]取的列表中的第一个元素:
[0,0,0,0,0,0,1]
4.calendar.monthcalendar(year,month)[0].index(1)返回1出现的位置
代码中判断该值为6,意思是,这个月的1号是星期6
'''
ifcalendar.monthcalendar(year,month)[0].index(1)==6:
#--------------------------------------------------
print'Date:%d-%d(year-month)'%(year,month)
print'Sun Mon Tue Wed Thu Fri Sat'
foreincalendar.monthcalendar(year,month):
print'%d %d %d %d %d %d %d'%(e[0],e[1],e[2],e[3],e[4],e[5],e[6])
#--------------------------------------------------
printcalendar.monthcalendar(year,month)
printcalendar.monthcalendar(year,month)[0]
printcalendar.monthcalendar(year,month)[0].index(1)
c+=1
printc
结果:
Date:1901-9(year-month)
Sun Mon Tue Wed Thu Fri Sat
0 0 0 0 0 0 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 0 0 0 0 0 0
[[0,0,0,0,0,0,1],[2,3,4,5,6,7,8],[9,10,11,12,13,14,15],[16,17,18,19,20,21,22],[23,24,25,26,27,28,29],[30,0,0,0,0,0,0]]
[0,0,0,0,0,0,1]
6
Date:1901-12(year-month)
Sun Mon Tue Wed Thu Fri Sat
0 0 0 0 0 0 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 0 0 0 0 0
[[0,0,0,0,0,0,1],[2,3,4,5,6,7,8],[9,10,11,12,13,14,15],[16,17,18,19,20,21,22],[23,24,25,26,27,28,29],[30,31,0,0,0,0,0]]
[0,0,0,0,0,0,1]
6
2
H. python以年-月-日的形式输入日期,如2019-12-4判断输入的日期与给定的日期是否为同一天
1.
s.split('年')
2.
b[1][:-1]
3.
'-'.join([a[0],b[0],c])
4.
if s==s1:
5.
else:
I. python判断给定的字符串是否是有效日期的方法
python判断日期是否有效使用strptime把字符串转换成date类型,如果正常转换,那么就是合格的日期类型:
举例如下:
正确转换的字符串:
import datetime
datetime.datetime.strptime('24052010', "%d%m%Y").date()
datetime.date(2010, 5, 24)
转换异常的字符串:
import datetime
datetime.datetime.strptime('32052010', "%d%m%Y").date()
datetime.date(2010, 5, 32)