如何检查所选日期是星期几? [英] How do i check if the selected date is of which day in the week?

查看:92
本文介绍了如何检查所选日期是星期几?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何使用if,else语句检查Calendar1中选择的日期是星期一,星期二,星期三,星期四,星期五,星期六还是星期日?
我想写这样的东西:

How do i check if the date selected in Calendar1 is a Monday, Tuesday, Wednesday, Thursday, Friday, Saturday or a Sunday using if, else statement?

As I want to write something like that:

if (//The selected date is a Monday)
        {
            Label6.Text = "Monday";
        }

        else if (//The selected date is a Tuesday)
        {
            Label6.Text = "Tuesday";
        }
.
.
.
.

推荐答案

实际上,您根本不需要执行if then语句.您可以简单地执行以下格式:
Actually, you wouldn''t need to do the if then statement at all. You can simply do a format:
Label6.Text = Calendar1.SelectedDate.ToString("dddd");


这样可以将一周中的整天都放在控件中.以下是您可以使用的其他日期格式:

http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx [ ^ ]


That would put the full day of the week in the control. Here are more date formats you could use:

http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx[^]


甚至不需要去麻烦
Don''t even need to go to that trouble
var x = DateTime.Now;
if(x.DayOfWeek == DayOfWeek.Tuesday);


我假设Calendar返回一个DataTime值.我不在ASP.NET中工作

这是确切的解决方案:


I am assuming that Calendar returns a DataTime value. I do not work in ASP.NET

This is the exact solution:

Label6.Text = Calendar1.SelectedDate.DayOfWeek.ToString();


这篇关于如何检查所选日期是星期几?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆