Saturday, January 13, 2018

ASP.NET : Get Month name, number, year and number of days from given date

In this article I am going to explain how to get Month name, number, year and number of days from selected date in

I want to get the month name, number, year and number of days in selected month.


HTML markup of webform:

<html xmlns="">
<head runat="server">
     <script src=""></script> 
<script src="JS/bootstrap-3.3.2.min.js"></script>
    <link href="CSS/bootstrap-3.3.2.min.css" rel="stylesheet" />
    <link href="Datepicker/css/bootstrap-datepicker3.css" rel="stylesheet" />
    <script src="Datepicker/js/bootstrap-datepicker.js"></script> 
   <style type="text/css">
        #custompop {
  position: absolute;
  top: 33px;
  right: -300px;
  margin: 0;
  padding: 0;
  width: 250px;
  height: 250px;
    <form id="form1" runat="server">
    <div style="margin:100px">
            <tr><td>Select date : </td><td><asp:TextBox ID="txtdate" runat="server"></asp:TextBox><div id="custompop"></div></td></tr>
                 <asp:Button ID="btnsubmit" runat="server" Text="Save"/></td></tr>
             <tr><td colspan="2">
                 <asp:Label ID="lblresult" runat="server" Visible="false"></asp:Label></td></tr>
     <script type="text/javascript">
         $('#txtdate').datepicker({autoclose: true, container: '#custompop' });

C# Code :

protected void btnsubmit_Click(object sender, EventArgs e)
        DateTime date = DateTime.Parse(txtdate.Text);
        int monthno = date.Month;
        int year = date.Year;
        string monthname = date.ToString("MMMM");
        int nodays = DateTime.DaysInMonth(year, monthno);
        lblresult.Visible = true;
        lblresult.Text = "Month Name : " + monthname + "<br/>Month No. : " + monthno + "<br/>Year : " + year + "<br/>Number Of days :" + nodays;
    } Code:

Protected Sub btnsubmit_Click(sender As Object, e As EventArgs) Handles btnsubmit.Click
        Dim date_ As DateTime = DateTime.Parse(txtdate.Text)
        Dim monthno As Integer = date_.Month
        Dim year As Integer = date_.Year
        Dim monthname As String = date_.ToString("MMMM")
        Dim nodays As Integer = DateTime.DaysInMonth(year, monthno)
        lblresult.Visible = True
        lblresult.Text = "Month Name : " & monthname & "<br/>Month No. : " & monthno & "<br/>Year : " & year & "<br/>Number Of days :" & nodays
    End Sub

No comments:

Post a Comment