简体   繁体   中英

How to get last month date from selected date in asp.net c#

selected date is (txtfromdate) : Dec 01 2016

to date:(txttodate): Dec 31 2016

how to calculate Dec 31 2016 from Dec 01 2016 in c# or javascript.

DateTime firstOfDecember = new DateTime(2016, 12, 1);
int lastDay = DateTime.DaysInMonth(firstOfDecember.Year, firstOfDecember.Month);

If you want to create DateTime object equivalent to last day of the month:

DateTime lastOfDecember = new DateTime(firstOfDecember.Year, firstOfDecember.Month, lastDay);

Use DaysInMonth method of DateTime : DateTime.DaysInMonth Method (Int32, Int32)

在C#中,您可以使用DaysInMonth

DateTime endOfMonth = new DateTime (year, month,DateTime.DaysInMonth(year, month));

By Using JQuery

var selectedData = txtfromdate;
var date = new Date(selectedData);
var lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0);

So, In lastDay variable has result.

By Using C#

DateTime now = new Date(txtfromdate);
var startDate = new DateTime(now.Year, now.Month, 1);
var endDate = startDate.AddMonths(1).AddDays(-1);

In endDate variable has lastDate.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM