簡體   English   中英

MatLab的“ dateshift(…)”在Python中是否等效?

[英]Equivalent function of MatLab's “dateshift(…)” in Python?

MatLab可以使用dateshift(...) 函數獲取日期並將其移動到月末,季度等。

Python中是否有等效功能?

我不確定它是否相同,但是datetime模塊可以將時間作為timetuple ,並具有用於年,月,日等的單獨組件。這很容易直接操作以前進到下個月,等等。

如果您不介意使用擴展名,請簽出dateutil 功能列表開始於:

  • 計算相對增量(下個月,明年,下周一,一個月的最后一周等);

dateutil.relativedelta的文檔顯示了如何升級到各個點。

我認為calendar.monthrange應該為您做到這一點,例如:

>>> import datetime, calendar
>>> year = int(input("Enter year: "))
Enter year: 2012
>>> month = int(input("Enter month: "))
Enter month: 3
>>> endOfMonthDate = datetime.date(year, month, calendar.monthrange(year, month)[1])
>>> endOfMonthDate
datetime.date(2012, 3, 31)
>>> 

您可能會在這里找到其他有用的功能: https : //docs.python.org/2/library/calendar.html和此處: https : //docs.python.org/2/library/datetime.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM