简体   繁体   English

如何获取日期时间月份开始和结束日期?

[英]How to get the Date time month start and End Date?

How to get the start date and end date of month in different variable. 如何在不同的变量中获取月份的开始日期和结束日期。 I have tried this and I get the start date but unable to find the end date 我试过这个,但是我得到了开始日期但无法找到结束日期

DateTime startDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).ToString("yyyy-MM-dd HH:mm:ss.fff");
DateTime  endDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddDays(30).ToString("yyyy-MM-dd HH:mm:ss.fff");

This logic fails when month end date is 31 and 28 or 29. Your Help are surely appretiated. 当月结束日期为31和28或29时,此逻辑将失败。您的帮助肯定是有用的。

您可以像这样计算endDate

DateTime endDate = startDate.AddMonths(1).AddDays(-1);

To get First Date 获得第一次约会

public DateTime FirstDayOfMonth(DateTime dateTime)
{
   return new DateTime(dateTime.Year, dateTime.Month, 1);
}

To get Last Date 获取最后日期

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

You already had the start date : 你已经有了开始日期:

DateTime monthStartDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);

There's a method to get the number of days in a month (and Looking at the IL code, it seems that this way is more efficient than the other answers, though unless you're going to do it a billion time, I doubt there will be any difference) : 有一种方法可以获得一个月的天数(看看IL代码,看起来这种方式比其他答案更有效,但除非你要做十亿次,我怀疑会有有任何区别):

int daysInMonth = DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month);
DateTime monthEndDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, daysInMonth);

For first date: 第一次约会:

DateTime first_date = new DateTime(DateTimePicker.Value.Year, DateTimePicker.Value.Month, 1);

For last date: 最后日期:

DateTime last_date = new DateTime(DateTimePicker.Value.Year, DateTimePicker.Value.Month, DateTime.DaysInMonth(DateTimePicker.Value.Year, DateTimePicker.Value.Month));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 获取特定月份的开始日期和结束日期 - Get Start Date and End date for certain month count 我想按开始日期、开始时间和结束日期、结束时间获取数据 - I want to get data by start date, start time and end date, end time 如何在SSRS中获取开始日期和结束日期之间的差的平均值 - How to get the average of the difference between start date and end date in SSRS 通过时区 ID 获取夏令时的开始和结束日期 - Get Start and end date of daylight savings time by Timezone ID 获取给定周,给定月份和给定周的开始和结束日期 - Get the start and end date of a given week year, given month & given week 确定两个日期范围是否重叠,如果是,我如何获得具有新的开始和结束时间的重叠时间 - Determine Whether Two Date Ranges Overlap, if yes how do I get the overlapped time with new start and end time 如何从给定日期获取下个月的结束日期 - How can I get the next month End Date from the given Date 如何从Commerce Server获取折扣的开始和结束日期? - How to get Discount Start and End Date from Commerce Server? 如何在C#中以正确的时间检索当前周的开始日期和结束日期? - How to retrieve start date and end date with correct time for current week in C#? 当我输入 1/9/2017 等 12 个月的开始日期时,如何根据 12 个月后的计算找到结束日期 - When I Enter start date like 1/9/2017 for 12 month so how to find the end date as per calculation of 12 months after
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM