在c#中获取上个月的第一天和最后一天的日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/591752/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get the previous month's first and last day dates in c#
提问by Jeremy Boyd
I can't think of an easy one or two liner that would get the previous months first day and last day.
我想不出一个简单的一两个班轮可以得到前几个月的第一天和最后一天。
I am LINQ-ifying a survey web app, and they squeezed a new requirement in.
我正在 LINQ-ifying 一个调查网络应用程序,他们提出了一个新的要求。
The survey must include all of the service requests for the previous month. So if it is April 15th, I need all of Marches request ids.
调查必须包括上个月的所有服务请求。所以如果是 4 月 15 日,我需要所有游行请求 ID。
var RequestIds = (from r in rdc.request
where r.dteCreated >= LastMonthsFirstDate &&
r.dteCreated <= LastMonthsLastDate
select r.intRequestId);
I just can't think of the dates easily without a switch. Unless I'm blind and overlooking an internal method of doing it.
如果没有开关,我就无法轻松想到日期。除非我是瞎子,忽略了这样做的内部方法。
采纳答案by andleer
var today = DateTime.Today;
var month = new DateTime(today.Year, today.Month, 1);
var first = month.AddMonths(-1);
var last = month.AddDays(-1);
In-line them if you really need one or two lines.
如果您确实需要一两行,则将它们串联起来。
回答by MikeW
The way I've done this in the past is first get the first day of this month
我过去这样做的方法是先获取本月的第一天
dFirstDayOfThisMonth = DateTime.Today.AddDays( - ( DateTime.Today.Day - 1 ) );
Then subtract a day to get end of last month
然后减去一天得到上个月的月底
dLastDayOfLastMonth = dFirstDayOfThisMonth.AddDays (-1);
Then subtract a month to get first day of previous month
然后减去一个月得到上个月的第一天
dFirstDayOfLastMonth = dFirstDayOfThisMonth.AddMonths(-1);
回答by Franci Penov
DateTime LastMonthLastDate = DateTime.Today.AddDays(0 - DateTime.Today.Day);
DateTime LastMonthFirstDate = LastMonthLastDate.AddDays(1 - LastMonthLastDate.Day);
回答by Maksym Gontar
DateTime now = DateTime.Now;
int prevMonth = now.AddMonths(-1).Month;
int year = now.AddMonths(-1).Year;
int daysInPrevMonth = DateTime.DaysInMonth(year, prevMonth);
DateTime firstDayPrevMonth = new DateTime(year, prevMonth, 1);
DateTime lastDayPrevMonth = new DateTime(year, prevMonth, daysInPrevMonth);
Console.WriteLine("{0} {1}", firstDayPrevMonth.ToShortDateString(),
lastDayPrevMonth.ToShortDateString());
回答by Amy B
If there's any chance that your datetimes aren't strict calendar dates, you should consider using enddate exclusion comparisons... This will prevent you from missing any requests created during the date of Jan 31.
如果您的日期时间可能不是严格的日历日期,您应该考虑使用结束日期排除比较......这将防止您错过在 1 月 31 日期间创建的任何请求。
DateTime now = DateTime.Now;
DateTime thisMonth = new DateTime(now.Year, now.Month, 1);
DateTime lastMonth = thisMonth.AddMonths(-1);
var RequestIds = rdc.request
.Where(r => lastMonth <= r.dteCreated)
.Where(r => r.dteCreated < thisMonth)
.Select(r => r.intRequestId);
回答by rp.
An approach using extension methods:
一种使用扩展方法的方法:
class Program
{
static void Main(string[] args)
{
DateTime t = DateTime.Now;
DateTime p = t.PreviousMonthFirstDay();
Console.WriteLine( p.ToShortDateString() );
p = t.PreviousMonthLastDay();
Console.WriteLine( p.ToShortDateString() );
Console.ReadKey();
}
}
public static class Helpers
{
public static DateTime PreviousMonthFirstDay( this DateTime currentDate )
{
DateTime d = currentDate.PreviousMonthLastDay();
return new DateTime( d.Year, d.Month, 1 );
}
public static DateTime PreviousMonthLastDay( this DateTime currentDate )
{
return new DateTime( currentDate.Year, currentDate.Month, 1 ).AddDays( -1 );
}
}
See this link http://www.codeplex.com/fluentdatetimefor some inspired DateTime extensions.
请参阅此链接 http://www.codeplex.com/fluentdatetime,了解一些受启发的 DateTime 扩展。
回答by Simon
using Fluent DateTime https://github.com/FluentDateTime/FluentDateTime
使用 Fluent DateTime https://github.com/FluentDateTime/FluentDateTime
var lastMonth = 1.Months().Ago().Date;
var firstDayOfMonth = lastMonth.FirstDayOfMonth();
var lastDayOfMonth = lastMonth.LastDayOfMonth();
回答by MartinLeo
The canonical use case in e-commerce is credit card expiration dates, MM/yy. Subtract one second instead of one day. Otherwise the card will appear expired for the entire last day of the expiration month.
电子商务中的典型用例是信用卡到期日期,MM/yy。减去一秒而不是一天。否则,该卡将在到期月份的最后一天显示为已到期。
DateTime expiration = DateTime.Parse("07/2013");
DateTime endOfTheMonthExpiration = new DateTime(
expiration.Year, expiration.Month, 1).AddMonths(1).AddSeconds(-1);
回答by MartinLeo
I use this simple one-liner:
我使用这个简单的单线:
public static DateTime GetLastDayOfPreviousMonth(this DateTime date)
{
return date.AddDays(-date.Day);
}
Be aware, that it retains the time.
请注意,它会保留时间。
回答by B. Clay Shannon
This is a take on Mike W's answer:
这是对 Mike W 的回答的看法:
internal static DateTime GetPreviousMonth(bool returnLastDayOfMonth)
{
DateTime firstDayOfThisMonth = DateTime.Today.AddDays( - ( DateTime.Today.Day - 1 ) );
DateTime lastDayOfLastMonth = firstDayOfThisMonth.AddDays (-1);
if (returnLastDayOfMonth) return lastDayOfLastMonth;
return firstDayOfThisMonth.AddMonths(-1);
}
You can call it like so:
你可以这样称呼它:
dateTimePickerFrom.Value = GetPreviousMonth(false);
dateTimePickerTo.Value = GetPreviousMonth(true);