Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 如何生成日期_Asp.net_Vb.net - Fatal编程技术网

Asp.net 如何生成日期

Asp.net 如何生成日期,asp.net,vb.net,Asp.net,Vb.net,我有一个表单,点击一个按钮,我会自动生成一个日期,正好是未来的一年。我想知道如何确保这个日期不是在公共假日或周末。需要帮忙吗 我希望将日期值存储在变量中,以便将其放置在command.Parameters中。AddWithValue@DueDate,您可以为此创建自己的逻辑。这很简单。创建一个检查日期是工作日还是假日的方法。但是你必须对假期进行硬编码,因为每个国家/大陆/文化等都不同 public bool IsWeekday(DateTime date) { int dayOfWeek

我有一个表单,点击一个按钮,我会自动生成一个日期,正好是未来的一年。我想知道如何确保这个日期不是在公共假日或周末。需要帮忙吗


我希望将日期值存储在变量中,以便将其放置在command.Parameters中。AddWithValue@DueDate,您可以为此创建自己的逻辑。这很简单。创建一个检查日期是工作日还是假日的方法。但是你必须对假期进行硬编码,因为每个国家/大陆/文化等都不同

public bool IsWeekday(DateTime date)
{
    int dayOfWeek = (int)date.DayOfWeek;

    //week starts on sunday
    if (dayOfWeek == 0 || dayOfWeek == 6)
    {
        return false;
    }
    else
    {
        return true;
    }
}


public bool IsHoliday(DateTime date)
{
    int currentYear = DateTime.Now.Year;

    //define your holidays here, they differ between cultures and continents etc
    List<DateTime> holidays = new List<DateTime>()
    {
        new DateTime(currentYear, 1, 1), //new years day
        new DateTime(currentYear, 1, 9), //for testing
        new DateTime(currentYear, 4, 27), //kings day
        new DateTime(currentYear, 6, 21), //longest day of the year
        new DateTime(currentYear, 12, 25), //christmas
        new DateTime(currentYear, 12, 26) //christmas
    };

    //check the date against the list of holidays
    if (holidays.Any(x => x == date.Date))
    {
        return true;
    }
    else
    {
        return false;
    }
}

周末部分很简单,因为DateTime值具有DayOfWeek属性。至于公共假日,没有什么可以做的,所以你只需要把日期与相关的公共假日日期的列表进行比较。那么你是说如果我使用“星期一”,那么它会自动忽略周末,只考虑周一到星期五。jmchilinneyno,你必须选择忽略哪几天。DayOfWeek只会让你知道约会是在一周中的哪一天;不要只是猜测它的作用或者问他们它的作用。去看看它对你自己有什么好处。然后应用一些逻辑说明如何在您的场景中使用它。逻辑在编程中的工作方式与在其他任何地方的工作方式相同。如果有人告诉你,他们计划在某个特定的日期去某个地方,而你不想让他们在周末去,你会自动地找出那个日期是星期几吗?为什么要在这里呢?这是一个vb.net问题。
//get a monday
DateTime monday = new DateTime(2019, 1, 7);

//loop all days of the week
for (int i = 0; i < 7; i++)
{
    DateTime nextDay = monday.AddDays(i);
    Label1.Text += string.Format("{0} - {1} - {2}<br>", nextDay.ToLongDateString(), IsWeekday(nextDay), IsHoliday(nextDay));
}
maandag 7 januari 2019 - True - False
dinsdag 8 januari 2019 - True - False
woensdag 9 januari 2019 - True - True
donderdag 10 januari 2019 - True - False
vrijdag 11 januari 2019 - True - False
zaterdag 12 januari 2019 - False - False
zondag 13 januari 2019 - False - False