如何在Python中的日期准确减去X月?

时间:2016-03-10 05:58:50

标签: python datetime

所以这个代码就是我想要的:

import datetime
d = datetime.date.today()

three_months_ago = d - timedelta(months=3)

然而,正如我们所知,'月' param在timedelta中不存在。

我承认我可以像这样编程来实现目标:

if d.month > 3:
    three_months_ago = datetime.date(d.year, d.month-3, d.day)
else:
    three_months_ago = datetime.date(d.year-1, d.month-3+12, d.day)

但这看起来真的很愚蠢......

你能告诉我如何巧妙地实现这个目标吗?

2 个答案:

答案 0 :(得分:2)

这可能有所帮助:

>>>from dateutil.relativedelta import relativedelta
>>>import datetime
>>>datetime.date.today()
datetime.date(2016, 3, 10)
>>>datetime.date.today() - relativedelta(months=3)
datetime.date(2015, 12, 10)

您也可以使用relativedelta()来添加或减去周和年。

答案 1 :(得分:0)

Numpy的timedelta有几个月的支持,即:

import java.util.ArrayList; public class test { ArrayList<String> stringOf100 = new ArrayList<String>(); ArrayList<String> stringOf200 = new ArrayList<String>(); public test() { for (int i = 1; i <= 200; i++) { stringOf200.add(i + ","); } for (int i = 1; i <= 100; i++) { stringOf100.add(i + ","); } stringOf100.size(); // = 100 for (String string : stringOf100) { System.out.println(string); } /* prints this. 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, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100 */ filter(stringOf100); // filter(stringOf200); } private void filter(ArrayList<String> strings) { int outputCommaValues = 20; //as i understood this is a constant of 20. "different strings each containing 20 comma separated values." int outputStrings = strings.size() / outputCommaValues; //5 or 10 or 5/100 = 0.05 -> 0.05*strings.size if strings.size = 100 -> 0.05*100 = 5 if strings.size = 200 -> 0.05*200 = 10. not sure what you want this to be ArrayList<String> output = new ArrayList<String>(); StringBuilder sb = new StringBuilder(); int c = 0; for (int i = 1; i <= outputStrings; i++) { for (int j = 1; j <= outputCommaValues; j++) { if (strings.get(c) != null) { sb.append(strings.get(c)); c++; } } output.add(sb.toString()); sb.delete(0, sb.length()); } System.out.println(" c: " + c); System.out.println(" size: " + output.size()); for (String string : output) { System.err.println(string); } /* stringOf100 outputs 5 strings: 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,32,33,34,35,36,37,38,39,40, 41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60, 61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80, 81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100, */ /* stringOf200 outputs 10 strings: 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,32,33,34,35,36,37,38,39,40, 41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60, 61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80, 81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100, 101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120, 121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140, 141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160, 161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180, 181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,198,199,200, */ } }