首页 > 解决方案 > for 循环的问题,并尝试将列表声明从列表字符串到日期时间的转换中分离出来

问题描述

因此,我正在尝试制作一个“PTO 计算器”,它将计算一年中剩余工作日的百分比与我剩余的 PTO 工作时间百分比。基本上,我可以使用一个应用程序来快速查看我是否在整个一年中过快地消耗了分配的带薪休假。这是我到目前为止所拥有的,

using System;
using System.Globalization;
using System.Linq;
class Program
{
static void Main()
{
    int PTOHoursTotal = 144;
    int PTOleft = 91;//user inputs the number of PTO hours remaining
    int workdays = 366;//the for loop below will run through the entire year and deduct 
    //holidays and weekends from 366 to give the total number of workdays in the year 
    var Holidays = new[]
    {
        "February 17, 2020", "May 25, 2020",
        "July 3, 2020", "September 7, 2020",
        "October 12, 2020", "November 11, 2020",
        "November 26, 2020", "December 25, 2020"
    }.Select(str => DateTime.ParseExact(str, "MMMM d, yyyy", CultureInfo.InvariantCulture));
    for (DateTime i = DateTime(2020, 1, 1); i < (2020, 12, 31); i.AddDays(1))
    {
        if (Holidays.Contains(i) || i.DayOfWeek == Saturday || i.DayOfWeek == Sunday)
        {
            workdays--;
        }
        Console.WriteLine(workdays);
    }
}

我相当肯定我的 for 循环有问题,最重要的是,如果有人可以展示一种方法,将声明的假日字符串列表从字符串转换为日期时间拆分,那将不胜感激。根据某个日期计算网站,最终的工作日数应该是 251。

标签: c#datetimestring-conversion

解决方案


推荐阅读