获取上个月的第一天和最后一天的日期在C#

问题描述:

我想不出一个简单的一两个班轮,将得到前几个月的第一天和最后一天。获取上个月的第一天和最后一天的日期在C#

我LINQ-ifying的一项调查显示Web应用程序,他们挤在一个新的要求。

调查必须包括所有前一个月的服务请求。所以如果是4月15日,我需要所有的Marches请求ID。

var RequestIds = (from r in rdc.request 
        where r.dteCreated >= LastMonthsFirstDate && 
        r.dteCreated <= LastMonthsLastDate 
        select r.intRequestId); 

我只是无法想象的日期很容易没有开关。除非我是盲目的,并且忽略了内部的做法。

var today = DateTime.Today; 
var month = new DateTime(today.Year, today.Month, 1);  
var first = month.AddMonths(-1); 
var last = month.AddDays(-1); 

如果你真的需要一两行,就可以联机。

我已经在过去做到了这一点的方式是先拿到这个月

dFirstDayOfThisMonth = DateTime.Today.AddDays(- (DateTime.Today.Day - 1)); 

然后第一天减去一天拿到上月底

dLastDayOfLastMonth = dFirstDayOfThisMonth.AddDays (-1); 

然后减去每月能拿到前一个月的第一天

dFirstDayOfLastMonth = dFirstDayOfThisMonth.AddMonths(-1); 
+0

网2.0,Today.Days不工作。 – 2009-02-26 18:22:37

+0

DateTime.Today.Day你的意思是 – 2009-02-26 18:34:56

+3

+1,但要迂腐,你最好评估一下DateTime.Today并存储在本地变量中。如果您的代码在午夜之前开始执行纳秒,则对DateTime.Today的两次连续调用可能会返回不同的值。 – Joe 2009-02-26 18:36:46

DateTime LastMonthLastDate = DateTime.Today.AddDays(0 - DateTime.Today.Day); 
DateTime LastMonthFirstDate = LastMonthLastDate.AddDays(1 - LastMonthLastDate.Day); 

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()); 

如果有你的日期时间没有严格的日历日期任何机会,你应该考虑使用结束日期排除比较... 这会防止你失踪一月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); 

的一种方法使用扩展方法:

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); 
    } 
} 

请参阅此链接 http://www.codeplex.com/fluentdatetime 为一些启发DateTime扩展。

编辑:这不是做的方法!
如果today.Month - 1 = 0,与1月一样,这将引发异常。这显然是聪明让你陷入麻烦的情况!

accepted answer稍微简单一些:

var today = DateTime.Today 
var first = new DateTime(today.Year, today.Month - 1, 1); 
var last = new DateTime(today.Year, today.Month, 1).AddDays(-1); 

节省额外的日期时间创造。

用流利的DateTime https://github.com/FluentDateTime/FluentDateTime

 var lastMonth = 1.Months().Ago().Date; 
     var firstDayOfMonth = lastMonth.FirstDayOfMonth(); 
     var lastDayOfMonth = lastMonth.LastDayOfMonth(); 

在电子商务的规范使用情况是信用卡到期日期,MM/YY。减去一秒而不是一天。否则,卡在过期月份的整个最后一天将出现过期。

DateTime expiration = DateTime.Parse("07/2013"); 
DateTime endOfTheMonthExpiration = new DateTime(
    expiration.Year, expiration.Month, 1).AddMonths(1).AddSeconds(-1); 

我用这简单的一行:

public static DateTime GetLastDayOfPreviousMonth(this DateTime date) 
{ 
    return date.AddDays(-date.Day); 
} 

要知道,它保留的时间。

这是迈克·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); 
} 

你可以把它像这样:

dateTimePickerFrom.Value = GetPreviousMonth(false); 
dateTimePickerTo.Value = GetPreviousMonth(true);