.Net C# Date and Time functions and Displaying the Month using a case statment.

//Note: Literal1 refers to a Literal Control that needs to be created on your form.

        protected void dateAndTime()
        {
            // Create a Time Object
            DateTime myTime = DateTime.Now;

            // Display the Year
            Literal1.Text = "The Year is: " + myTime.Year;


            // Display the Time
            Literal1.Text += "<br />  The Time is: ";
            Literal1.Text += myTime.Hour + ":" + myTime.Minute + ":" + myTime.Second;


            // Display the Month
            Literal1.Text += "<br /> The Month is: ";
            String thisMonth;
            thisMonth = getMonth(myTime.Month); // Call the Month Function
            Literal1.Text += myTime.Month + " (" + thisMonth + ")";
          
        }


        String  getMonth(int monthIn)
        {
            int caseSwitch = monthIn;
            string monthOut;
            switch (caseSwitch)
            {
                case 1:
                    monthOut = "January";
                    break;
                case 2:
                    monthOut = "February";
                    break;
                case 3:
                    monthOut = "March";
                    break;
                case 4:
                    monthOut = "April";
                    break;
                case 5:
                    monthOut = "May";
                    break;
                case 6:
                    monthOut = "June";
                    break;
                case 7:
                    monthOut = "July";
                    break;
                case 8:
                    monthOut = "August";
                    break;
                case 9:
                    monthOut = "September";
                    break;
                case 10:
                    monthOut = "October";
                    break;
                case 11:
                    monthOut = "November";
                    break;
                case 12:
                    monthOut = "November";
                    break;
                default:
                    monthOut = ("");
                    break;
            }
            return monthOut;
        }

Comments

Popular posts from this blog

Umbraco - Reset the Admin Password via SQL Server

Create a .NET Contact Form that Gets the Last Url Visited in C# Can also be Used in Umbraco

Umbraco Macro for IFrame when Embedding Video, Vimeo Universal Player, or other types of IFrame on to a Page XSLT version and Razor Version