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 asp.net.


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

Implementation:

HTML markup of webform:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
     <script src="https://code.jquery.com/jquery-3.2.1.min.js"></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;
}
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div style="margin:100px">
        <table>
            <tr><td>Select date : </td><td><asp:TextBox ID="txtdate" runat="server"></asp:TextBox><div id="custompop"></div></td></tr>
             <tr><td>&nbsp;</td><td></td></tr>
             <tr><td></td><td>
                 <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>
        </table>       
    </div>
    </form>
     <script type="text/javascript">
         $('#txtdate').datepicker({autoclose: true, container: '#custompop' });
        </script>
</body>
</html>


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;
    }


VB.net 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