Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the Date time month start and End Date?

Tags:

date

c#

datetime

How to get the start date and end date of month in different variable. I have tried this and I get the start date but unable to find the end date

DateTime startDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).ToString("yyyy-MM-dd HH:mm:ss.fff");
DateTime  endDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddDays(30).ToString("yyyy-MM-dd HH:mm:ss.fff");

This logic fails when month end date is 31 and 28 or 29. Your Help are surely appretiated.

like image 730
Iswar Avatar asked Apr 15 '14 07:04

Iswar


People also ask

How do you check the date is last day of month in C #?

AddMonths(1). AddDays(-3);". (-3) is the day amount so 0 of next month is basicly the last day of current month.

How do I get the first date of the month in VB net?

Getting Started The solution contains a single Windows Forms project called FirstLastDayVB which was written in VB.NET; the application contains a single Windows form (Form1. vb); this form contains all of the code necessary to calculate and display the first and last days of the month.


2 Answers

You can calculate endDate like this:

DateTime endDate = startDate.AddMonths(1).AddDays(-1);
like image 131
Ulugbek Umirov Avatar answered Oct 11 '22 16:10

Ulugbek Umirov


DateTime endDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1)
                       .AddMonths(1).AddDays(-1);
like image 38
Janty Avatar answered Oct 11 '22 18:10

Janty