从ASP标签获取DayOfWeek值

时间:2019-03-26 14:16:47

标签: c# asp.net datetime

我有一个ASP标签lblweekoff.Text及其值可以是任意2个工作日的组合,例如Sun,Sat / Sun,Mon / Mon,Tue等,并且我想获取2个工作日拆分的DayOfWeek值用逗号检查日期文本框txtdate.Text。

例如

让我们说我的标签的价值如下

lblweekoff.Text = "Sun,Sat";
txtDate.Text = "30/3/2019";
var dayColumns = new[] { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
DateTime weekday = Convert.ToDateTime(lblweekoff.Text);
var weekoff = dayColumns[(int)weekday.DayOfWeek];
DateTime Date = Convert.ToDateTime(txtDate.Text);
if(Date.DayOfWeek == weekoff)
{
    //some code
}
else
{
    //some code
}

因此,我想检查在文本框DayOfWeek上选择的日期txtDate.Text是否等于用逗号DayOfWeeks分隔的两个Sun,Sat值之一。如标签lblweekoff.Text上所示,并根据此进一步操作。

我该如何完成?感谢您的帮助,谢谢。

3 个答案:

答案 0 :(得分:2)

我已将所有控件转换为字符串,您需要再次将其更改为您的控件。

    string lblweekoff = "Sun,Sat";
    string txtDate = "30/Mar/2019"; // you might need to format this date 

    var dayColumns = new[] { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
    var offDay = lblweekoff.Split(',').ToList();
    DateTime Date = Convert.ToDateTime(txtDate);
    if (offDay.Contains(Date.ToString("ddd")))// weekend
    {
        //some code
    }
    else
    {
        //some code
    }

答案 1 :(得分:1)

首先,我不太了解您的要求,但这可能对您有用。

您今天就可以得到

string day = DateTime.Today.DayOfWeek.ToString();
lblweekoff.Text = day;

结束时,您可以随机选择其他一个,

Random rnd = new Random();
int day2= rnd.Next(0, 6);
lblweekoff.Text =lblweekoff.Text + ","+dayColumns[rnd];

我希望这会有所帮助,

答案 2 :(得分:1)

        string lblweekoff = "Sun,Sat";
        string txtDate = "3/30/2019";// need format "m/dd/yyyy"
        var dayColumns = new[] { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
        List<string> weekday = lblweekoff.Split(',').ToList();
        if( dayColumns.Contains(Convert.ToDateTime(txtDate).ToString("ddd")))
        {

        }