導航:首頁 > 編程語言 > python判斷是否為日期

python判斷是否為日期

發布時間:2022-12-19 05:09:01

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))

解析效果:

  1. 解析前:

    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

  2. 解析後:

    統一變為標准時間格式: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)

閱讀全文

與python判斷是否為日期相關的資料

熱點內容
dvd光碟存儲漢子演算法 瀏覽:757
蘋果郵件無法連接伺服器地址 瀏覽:963
phpffmpeg轉碼 瀏覽:672
長沙好玩的解壓項目 瀏覽:145
專屬學情分析報告是什麼app 瀏覽:564
php工程部署 瀏覽:833
android全屏透明 瀏覽:737
阿里雲伺服器已開通怎麼辦 瀏覽:803
光遇為什麼登錄時伺服器已滿 瀏覽:302
PDF分析 瀏覽:485
h3c光纖全工半全工設置命令 瀏覽:143
公司法pdf下載 瀏覽:382
linuxmarkdown 瀏覽:350
華為手機怎麼多選文件夾 瀏覽:683
如何取消命令方塊指令 瀏覽:350
風翼app為什麼進不去了 瀏覽:778
im4java壓縮圖片 瀏覽:362
數據查詢網站源碼 瀏覽:150
伊克塞爾文檔怎麼進行加密 瀏覽:892
app轉賬是什麼 瀏覽:163