Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/279.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.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
如何将两个日期转换为月份格式c#_C#_Date - Fatal编程技术网

如何将两个日期转换为月份格式c#

如何将两个日期转换为月份格式c#,c#,date,C#,Date,假设我们有两个日期2012-10-10和2012-12-31。我想在这样的表中输入数据 开始日期结束日期 2012-10-10 2012-10-31 2012-11-01 2012-11-30 2012-12-01 2012-12-31 任何代码或算法都将受到欢迎。类似以下内容: class Program { public static DateTime FirstDayOfMonthFromDate

假设我们有两个日期2012-10-10和2012-12-31。我想在这样的表中输入数据

开始日期结束日期
2012-10-10               2012-10-31
2012-11-01               2012-11-30
2012-12-01               2012-12-31
任何代码或算法都将受到欢迎。

类似以下内容:

class Program
{
    public static DateTime FirstDayOfMonthFromDateTime(DateTime dateTime)
    {
        return new DateTime(dateTime.Year, dateTime.Month, 1);
    }

    public static DateTime LastDayOfMonthFromDateTime(DateTime dateTime)
    {
        DateTime firstDayOfTheMonth = new DateTime(dateTime.Year, dateTime.Month, 1);
        return firstDayOfTheMonth.AddMonths(1).AddDays(-1);
    }

    static void Main(string[] args)
    {
        var date1 = new DateTime(2012, 10, 10);
        var date2 = new DateTime(2012, 12, 31);
        Console.Out.WriteLine(date1.ToShortDateString() + "\t" + LastDayOfMonthFromDateTime(date1).ToShortDateString());
        while (LastDayOfMonthFromDateTime(date1) < date2)
        {
            date1 = date1.AddMonths(1);
            Console.Out.WriteLine(FirstDayOfMonthFromDateTime(date1).ToShortDateString() + "\t" + LastDayOfMonthFromDateTime(date1).ToShortDateString());
        }

        Console.ReadLine();
    }
}

(我从中提取了第一个/最后一个函数。)

你不能只索取代码。。你尝试了什么?你在哪里卡住了?你想做什么?请描述更多。
2012/10/10      2012/10/31
2012/11/01      2012/11/30
2012/12/01      2012/12/31