C#把日期转化成星期

显示效果:

      *****     

前台页面代码:

1 <TextBlock Grid.Row="0" x:Name="lb_txt_0" Text="选中日期" FontSize="13"/>
2 <TextBlock Grid.Row="1" x:Name="lb_txt_1" Text="哪年" FontSize="13"/>
3 <TextBlock Grid.Row="2" x:Name="lb_txt_2" Text="哪月" FontSize="13"/>
4 <TextBlock Grid.Row="3" x:Name="lb_txt_3" Text="当月第几天" FontSize="13"/>
5 <TextBlock Grid.Row="4" x:Name="lb_txt_4" Text="星期几" FontSize="13"></TextBlock>
6 <TextBlock Grid.Row="5" x:Name="lb_txt_5" Text="当月总天数" FontSize="13"></TextBlock>
7 <TextBlock Grid.Row="6" x:Name="lb_sum" Text="所有日期" FontSize="13"></TextBlock>
View Code

后台代码:

 1             //指定日期
 2             DateTime selectTime = Convert.ToDateTime(rdp_schedule.SelectedValue);
 3             lb_txt_0.Text = "选中时间:" + selectTime;
 4             //哪年
 5             int year = selectTime.Year;
 6             lb_txt_1.Text = "哪年:" + year + "";
 7             //哪月
 8             int month = selectTime.Month;
 9             lb_txt_2.Text = "哪月:" + month + "";
10             //第几天
11             int day = selectTime.Day;
12             lb_txt_3.Text = "当月第几天:" + day;
13 
14             // 星期几
15             string week_EN = selectTime.DayOfWeek.ToString();//英文星期几
16             string[] weekRemarks = new string[] { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
17             string week_CN = weekRemarks[Convert.ToInt16(selectTime.DayOfWeek)];//中文星期几
18             lb_txt_4.Text = "星期几:" + week_EN + "[" + week_CN + "]";
19 
20             //一个月的总天数
21             int totalDays = DateTime.DaysInMonth(year, month);
22             lb_txt_5.Text = "该月天数:" + totalDays;
23 
24             //显示该月所有日期是星期几
25 
26             string timedays = "";
27             for (int i = 1; i <= totalDays; i++)
28             {
29                 string cols = i + "号/" + week_str[Convert.ToInt16(selectTime.AddDays(i - selectTime.Day).DayOfWeek)];
30                 timedays += "该月第" + i + "天是" + selectTime.AddDays(i - selectTime.Day).DayOfWeek;
31                 timedays += "  // "+week_str[Convert.ToInt16(selectTime.AddDays(i - selectTime.Day).DayOfWeek)] + "
";
32             }
33 
34             lb_sum.Text = timedays;
View Code
原文地址:https://www.cnblogs.com/KLLQBKY/p/7002237.html