Get month from DateTime
The following example code demonstrate us how can we get the month part/component from a dateTime
object programmatically at run time. .Net framework's DateTime.Month property allow us to get the month part of the
date represented by this instance. DateTime is exists in .Net Framework's System namespace.
DateTime.Month property return a System.Int32 data type value. This return integer number represent the month component of the provided DateTime instance. This return integer value is a number between 1 to 12 where 1 represent January, 2 represent February and so on. The provided output image help you more to better understand this tutorial.
DateTime.Month property return a System.Int32 data type value. This return integer number represent the month component of the provided DateTime instance. This return integer value is a number between 1 to 12 where 1 represent January, 2 represent February and so on. The provided output image help you more to better understand this tutorial.
Month.aspx
<%@ Page Language="C#" %>
<!DOCTYPE html>
<script runat="server">
protected void Button1_Click(object sender, System.EventArgs e) {
DateTime myDateTime = DateTime.Now;
string month = myDateTime.Month.ToString();
Label1.Text = "My DateTime: " + DateTime.Now.ToLongDateString();
Label1.Text += "<br />Month: " + month;
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>asp.net date time example: how to get month from a date time object in asp.net</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:Navy">asp.net date time example: Month</h2>
<asp:Label
ID="Label1"
runat="server"
Font-Size="Larger"
Font-Italic="true"
Font-Bold="true"
ForeColor="Violet"
>
</asp:Label>
<br /><br />
<asp:Button
ID="Button1"
runat="server"
Font-Bold="true"
ForeColor="Violet"
OnClick="Button1_Click"
Text="Get Month From DateTime Object"
/>
</div>
</form>
</body>
</html>

- How to get days between two DateTimes
- How to subtract two DateTimes
- How to get year from a date
- How to get the AM PM value from a DateTime object
- How to check if a DateTime is between two given dates
- How to get the last day of month using a given date
- How to get the month name from month number
- How to get the first day of week
- How to get the integer value of day of week
- How to get two DateTime objects difference in milliseconds