自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

對(duì)比Excel,怎么用Python獲取指定月最后一天的日期

開(kāi)發(fā) 后端
這是群友的一個(gè)需求。他想根據(jù)一個(gè)給定的日期,獲取該給定日期在這個(gè)年月的最后一天的日期,并且要求在Python中怎么做?;诖?,我就想到了對(duì)比Excel,在Python中完成這個(gè)需求。

 [[335095]]

這是群友的一個(gè)需求。他想根據(jù)一個(gè)給定的日期,獲取該給定日期在這個(gè)年月的最后一天的日期,并且要求在Python中怎么做?;诖?,我就想到了對(duì)比Excel,在Python中完成這個(gè)需求。

 

Excel完成這個(gè)需求

① 在單元格中輸入EOMONTH()公式

 

② 完成填充操作

 

③ 將時(shí)間戳轉(zhuǎn)換為短日期格式

可以看到在excel中輸入該公式后,默認(rèn)返回的是一個(gè)時(shí)間戳,我們需要將其轉(zhuǎn)換為我我們需要的短日期格式。

 

Python完成這個(gè)需求

在完成這個(gè)需求之前,我們先來(lái)講述幾個(gè)知識(shí)點(diǎn),否則你會(huì)看著懵逼。

1)datetime庫(kù)中的知識(shí)點(diǎn)

 

  1. import datetime 
  2.  
  3. date1 = datetime.date.today() 
  4. print(date1) 
  5.  
  6. year = datetime.date.today().year 
  7. print(year)` 
  8.  
  9. month = datetime.date.today().month 
  10. print(month

結(jié)果如下:

 

可以看出:上面我們使用today()函數(shù)直接獲取了當(dāng)前系統(tǒng)的年月,并且利用year和month屬性分別獲取了當(dāng)前系統(tǒng)時(shí)間的年、月。

接下來(lái),我們使用該模塊獲取指定年、月的指定日期,其中day=1表示獲取當(dāng)前年月的第一天,day=2表示獲取當(dāng)前年月的第二天......依次進(jìn)行下去。

 

  1. date1 = datetime.date(year=yearmonth=monthday=1) 
  2. print(date1) 
  3.  
  4. date2 = datetime.date(year=yearmonth=monthday=2) 
  5. print(date2) 

結(jié)果如下:

 

通過(guò)上述演示,應(yīng)該可以知道怎么完成這個(gè)需求了吧?當(dāng)我們指定了年月,后面只需要傳入day=當(dāng)月的總天數(shù)這個(gè)參數(shù),得到的不就是該年月的最后一天的日期嗎?因此目前的問(wèn)題就是怎么獲取某個(gè)月的最后一天,這就是下面需要講述的monthrange()函數(shù)。

2)calendar庫(kù)中的知識(shí)點(diǎn)

對(duì)于這個(gè)模塊,我主要講述一個(gè)monthrange()函數(shù),這個(gè)函數(shù)是有什么用呢?

  • monthrange(year, month):返回指定年月,由第一天所在的星期和本月的總天數(shù)組成的元組。

 

  1. # 這里的yearmonth就是上面提到的2020和7 
  2. import calendar 
  3.  
  4. x, y = calendar.monthrange(yearmonth
  5. print(x, y) 
  6. ""
  7. 其中:x表示2020/7/1號(hào)所在的星期數(shù),其中周一是0,周二是1,周三是2。 
  8.      y表示的是2020年7月這個(gè)月的總天數(shù)。 
  9. ""

結(jié)果如下:

 

3)完整代碼如下

 

  1. def func(year=None, month=None): 
  2.     if year
  3.         year = int(year
  4.     else
  5.         year = datetime.date.today().year 
  6.  
  7.     if month
  8.         month = int(month
  9.     else
  10.         month = datetime.date.today().month 
  11.  
  12.     # x:表示當(dāng)月第一天所屬的星期 
  13.     # y:表示當(dāng)月的總天數(shù) 
  14.     x, y = calendar.monthrange(yearmonth
  15.  
  16.     # 獲取當(dāng)月的第一天和最后一天 
  17.     first_day = datetime.date(year=yearmonth=monthday=1) 
  18.     last_day = datetime.date(year=yearmonth=monthday=y) 
  19.  
  20.     return first_day, last_day 
  21.  
  22. first_day, last_day = func() 
  23. print(first_day, last_day) 
  24.  
  25. first_day, last_day = func(2020,6) 
  26. print(first_day, last_day) 

結(jié)果如下:

 


這個(gè)代碼其實(shí)很好理解:如果year和month為空,就返回的是當(dāng)前系統(tǒng)時(shí)間的最后一天的日期。如果傳入指定年月,那么獲取的就是指定年月的最后一天的日期。

 

責(zé)任編輯:華軒 來(lái)源: 數(shù)據(jù)分析與統(tǒng)計(jì)學(xué)之美
相關(guān)推薦

2018-06-20 09:35:43

碼農(nóng)科技開(kāi)發(fā)

2021-05-05 09:46:49

WiFi路由器網(wǎng)絡(luò)

2019-04-28 09:56:15

程序員互聯(lián)網(wǎng)脫發(fā)

2024-12-09 09:41:42

javaScriptdate日期

2010-09-14 16:18:00

SQL Server日

2020-02-13 10:54:29

源碼模式Mybatis

2021-02-03 21:15:44

Ansible系統(tǒng)運(yùn)維系統(tǒng)管理員

2021-05-10 09:57:40

Windows10操作系統(tǒng)微軟

2012-05-16 11:47:40

Gmail郵件

2013-01-08 13:32:31

SEO GTD

2019-11-07 15:30:00

EmacsIDE

2016-10-17 18:28:03

2022-04-11 11:38:44

Python代碼游戲

2015-10-29 11:36:45

Google技術(shù)經(jīng)理程序員

2024-09-18 07:50:00

超算AI

2022-01-09 17:35:06

引擎規(guī)則數(shù)據(jù)

2012-08-10 22:44:52

ArchSummit

2021-05-11 09:52:13

AI 數(shù)據(jù)人工智能

2017-03-21 21:17:50

大數(shù)據(jù)數(shù)據(jù)互聯(lián)網(wǎng)

2010-04-13 16:09:43

谷歌70天最后
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)