计算下个月的最后一天

时间:2015-12-17 05:06:38

标签: c# datetime

我试图在C#中使用DateTime函数来计算下个月的最后一天。

例如,今天是2015年12月17日。我希望DateTime功能返回2016年1月31日(下个月的最后一天)。

我使用以下内容计算下个月的第一天(这是有效的):

DateTime firstDayNextMonth = DateTime.Today.AddDays(-DateTime.Now.Day+1).AddMonths(1);

4 个答案:

答案 0 :(得分:1)

//system date or any date u want this case it is a calendar picker - 22/03/2016
DateTime today = dtpFrom.Value;

//Add a month to your date example , it now becomes - 22/04/2016
DateTime endOfMonth = new DateTime(today.Year, today.Month,today.Day).AddMonths(1);

//Get the last date off the above which is - 30
int getlastday = DateTime.DaysInMonth(endOfMonth.Year, endOfMonth.Month);

//Now set the date to the value  which will be the last day off the next month - 30/04/2016
DateTime newDate = new DateTime(endOfMonth.Year, endOfMonth.Month, getlastday);

答案 1 :(得分:0)

试试这个:

1010:               final FileInputStream fileInputStream = new FileInputStream(new File(string));
                    FileUpload.this.uploadTimeStart = System.currentTimeMillis();
                    final HttpURLConnection httpURLConnection = (HttpURLConnection) new URL(uploadURL).openConnection();
                    httpURLConnection.setDoInput(true);
                    httpURLConnection.setDoOutput(true);
                    httpURLConnection.setUseCaches(false);
                    httpURLConnection.setRequestMethod("POST");
                    httpURLConnection.setRequestProperty("Connection", "Keep-Alive");
                    httpURLConnection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + "*****");
                    final File file = new File(FileUpload.this.rootDir + FileUpload.this.folderName, FileUpload.this.fileName);
                    final FileInputStream fileInputStream2 = new FileInputStream(file);
1021                final byte[] array2 = new byte[(int) file.length()];
                    fileInputStream2.read(array2);
                    fileInputStream2.close();
                    fileInputStream.close();

答案 2 :(得分:0)

DateTime.Now.AddMonths(1)

@ Ben:1会将11个月添加到当前日期,而不是减去DateTime.Now.AddMonths(1).Year个月。

enter image description here

20162015而非radians参考附加图片

答案 3 :(得分:0)

DateTime.DaysInMonth(DateTime.Now.AddMonths(1).Year, DateTime.Now.AddMonths(1).Month);