Asked by:
Get last day of month

Question
-
User66371569 posted
Hi there
I have textbox for inserting date called startdate
i want when user insert date get last day of month in another textbox
forexample
startdate: 23/09/2018 "my format is dd/mm/yyyy"
lastdate: 30/09/2018
thank u in advanceSunday, September 23, 2018 4:06 PM
All replies
-
User409696431 posted
I assume you know how to parse that for the integer month and year. Use DateTime.DaysInMonth(intyear, intmonth) https://docs.microsoft.com/en-us/dotnet/api/system.datetime.daysinmonth?view=netframework-4.7.2 to get the last day of that month (it returns an integer). You can then use that to construct the ending date in the format you want.
Monday, September 24, 2018 2:10 AM -
User-271186128 posted
Hi Sir,
You could use date methods to get the last day of month, code as below:
<input type="text" id="startdate" value="" /> <input type="text" id="enddate" value="" /> <script src="~/lib/jquery/dist/jquery.js"></script> <script type="text/javascript"> $(function () { $("#startdate").change(function () { debugger; var startdate = $(this).val(); var datenum = startdate.split('/'); //dd/mm/yyyy var date = new Date(parseInt(datenum[2]), parseInt(datenum[1]), 0); var yy = date.getFullYear(); //get year var mm = date.getMonth() + 1; //get month var day = date.getDate(); //get the day of the month $("#enddate").val(day + "/" + mm + "/" + yy); }); }); </script>
Best regards,
DillionMonday, September 24, 2018 3:20 AM -
User66371569 posted
Can u give me c# code if u can Thank alotMonday, September 24, 2018 4:03 AM -
User-369506445 posted
hi
please try below code
DateTime today = DateTime.Today; DateTime endOfMonth = new DateTime(today.Year, today.Month, DateTime.DaysInMonth(today.Year,today.Month)); Response.Write(today.ToString() +"<br />"); Response.Write(endOfMonth.ToString());
Result
9/24/2018 12:00:00 AM 9/30/2018 12:00:00 AM
Monday, September 24, 2018 5:14 AM -
User-271186128 posted
Hi sir,
You could refer to the following code:
<asp:TextBox ID="StartDate" runat="server" AutoPostBack="true" OnTextChanged="StartDate_TextChanged"></asp:TextBox> <asp:TextBox ID="LastDate" runat="server"></asp:TextBox>
and code behind:
protected void StartDate_TextChanged(object sender, EventArgs e) { var numlist = StartDate.Text.Split('/'); //get the number list. //base on the startdate to new date. DateTime date = new DateTime(Convert.ToInt32(numlist[2]), Convert.ToInt32(numlist[1]) + 1, 1); date = date.AddDays(-1); //get the last day LastDate.Text = date.ToString("dd/MM/yyyy"); }
In my opinion, I suggest you could use jquery to get the last date, because there is no need to send a request to server side to get the last date.
Best regards,
DillionMonday, September 24, 2018 8:03 AM