asp.net in C # get the day of the Week program code

Source: Internet
Author: User
Tags datetime error handling

Example

The code is as follows Copy Code

Lbday.text = System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.GetDayName (DateTime.Now.DayOfWeek);

Example 1

The code is as follows Copy Code

string[] Day = new string[] {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
String week = Day[convert.toint32 (DateTime.Now.DayOfWeek.ToString ("D"))]. ToString ();
Lbday.text = week;


Example 2

Output week by number ordinal

The code is as follows Copy Code

Select.cs program content: Through the input digital 0~6, the output week each day English words to demonstrate if statement and switch statement usage
Using System;
Class Select
{
public static void Main ()
{
Console.WriteLine ("Please enter a number:");
Console.WriteLine ("1~6 says Monday to Saturday; 0 means Sunday");
string s = Console.ReadLine ();
int n = Int. Parse (s);
if (n<0 | | n>6)//Judge input time legal
{
Console.WriteLine ("Input Error"); Error handling
}
else//input is correct, processed according to the selection
{
Switch (n)
{
Case 1://input as 1 o'clock processing
Console.WriteLine ("Monday is Monday");
Break After processing, jump out of switch
Case 2:
Console.WriteLine ("Tuesday is Tuesday");
Break
Case 3:
Console.WriteLine ("Wednesday is Wednesday");
Break
Case 4:
Console.WriteLine ("Thursday is Thursday");
Break
Case 5:
Console.WriteLine ("Friday is Friday");
Break
Case 6:
Console.WriteLine ("Saturday is Saturday");
Break
Case 0:
Console.WriteLine ("Sunday is Sunday");
Break
Default://In fact, before the input has been processed, the following statement cannot be reached
Console.WriteLine ("It is a crazy day!"); In this example, the default usage,
Break The reader can try to remove the IF statement and enter an illegal result.
}
}
}
}

Example 3

Calculate the day of the week according to the month

The code is as follows Copy Code

//y-year, M-month, D-Date
  string caculateweekday (int y,int m, int d)
  {
  if (m==1) m=13;
  if (m==2) m=14 ;
        int week= (d+2*m+3* (m+1)/5+y+y/4-y/100+y/400)%7;
  String weekstr= "";
   Switch (week)
   {
    case 1:weekstr= ' Monday '; break;
  & nbsp Case 2:weekstr= "Tuesday"; Break
    case 3:weekstr= "Wednesday";
    case 4:weekstr= "Thursday";
    case 5:weekstr= "Friday";
    case 6:weekstr= "Saturday";
    case 7:weekstr= "Sunday";
  }
          return weekstr;
 }

Use method


Label1.text=caculateweekday (2004,12,9);

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.