【问题标题】:Get the Last day of month in a string [duplicate]获取字符串中的最后一天[重复]
【发布时间】:2015-05-13 16:35:58
【问题描述】:

您好,我可以使用此代码获取本月的第一天:

static string FirstMonth = DateTime.Today.AddDays(1 - DateTime.Today.Day).ToString("yyyy-MM-dd");

如何以相同的日期格式获取月份的最后一天?

谢谢

【问题讨论】:

    标签: c#


    【解决方案1】:
    DateTime d = Convert.ToDateTime("2/4/15");
    
    DateTime lastDayOfSameMonth = (new DateTime(d.Year, d.Month, 1)).AddMonths(1).AddDays(-1);
    

    【讨论】:

    • 请在创建答案时使用format code 选项。这将使帖子看起来更好。添加一些解释和可能的文档引用也是一个好主意。这不是一项要求,但具有良好格式、解释和参考的令人愉悦的帖子更有可能获得支持和(或)被接受。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-04
    • 1970-01-01
    • 2011-12-20
    • 2018-03-10
    • 1970-01-01
    • 2017-09-06
    • 2018-02-06
    相关资源
    最近更新 更多