locked
First and Last Days RRS feed

  • Question

  • User265788195 posted

    I need to find the first and last day of the current month using C#?

    Wednesday, October 5, 2011 8:43 PM

Answers

  • User1178876220 posted
     DateTime dt = DateTime.Now;
    DateTime fd = Convert.ToDateTime(dt.Month + "/1/" + dt.Year);
    DateTime ld = Convert.ToDateTime(dt.Month + "/" + System.DateTime.DaysInMonth(dt.Year, dt.Month) + "/" + dt.Year);
    Response.Write(fd.DayOfWeek.ToString() + " " + ld.DayOfWeek.ToString());
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 5, 2011 10:59 PM
  • User1888482730 posted

    Here you go, let me know if its what you were looking for:

    private DateTime FirstDayOfMonth(DateTime dt){
                return new DateTime(dt.Year, dt.Month, 1);
            }
    
            private DateTime LastDayOfMonth(DateTime dt) {
                return FirstDayOfMonth(dt).AddMonths(1).AddDays(-1);
            }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 6, 2011 2:38 AM
  • User-1673382244 posted
    In this code we can get current month last date and first date , using C#.Net
    
    DateTime firstDay= new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
    
     int DaysinMonth = DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month)-1;
    
    DateTime lastDay = firstDay.AddDays(DaysinMonth);
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 6, 2011 3:21 AM
  • User3690988 posted

    This is in VB, but it will return the first and last second of the month.

        ' DATEADD(ms, -5, DATEADD(mm, DATEDIFF(m, 0, GETDATE()) + 1, 0))  - Taken from a T-SQL query to find last second of the month
    
        DATEDIF = DateDiff(DateInterval.Month, #1/1/1900#, DateTime.Now)
        firstDay = DateAdd(DateInterval.Month, DATEDIF, #1/1/1900#)
        lastDay = DateAdd(DateInterval.Month, DATEDIF + 1, #1/1/1900#).AddMilliseconds(-1)
        Label1.Text = firstDay.ToString & " - " & lastDay.ToString
    

    The label will print 10/1/2011 12:00:00 AM - 10/31/2011 11:59:59 PM

    Untested, converted c# code:

    DateTime firstDay = default(DateTime);
    DateTime lastDay = default(DateTime);
    long DATEDIF = 0;
    DATEDIF = DateDiff(DateInterval.Month, 1/1/1900 12:00:00 AM, DateTime.Now);
    firstDay = DateAdd(DateInterval.Month, DATEDIF, 1/1/1900 12:00:00 AM);
    lastDay = DateAdd(DateInterval.Month, DATEDIF + 1, 1/1/1900 12:00:00 AM).AddMilliseconds(-1);
    Label1.Text = firstDay.ToString + " - " + lastDay.ToString;
    

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 6, 2011 7:43 AM

All replies

  • User-1946294156 posted

    There is not a definative way of doing this.  If you are looking for the weekday, then you can build the date, for example, New DateTime(year, month, 1) for the first day of the month.

    Then for the last Day of the Month, New DateTime(year, month, DateTime.DaysInMonth(Year, Month).

    Then you can do use the day of week function to get the day. 

    Wednesday, October 5, 2011 9:14 PM
  • User1178876220 posted
     DateTime dt = DateTime.Now;
    DateTime fd = Convert.ToDateTime(dt.Month + "/1/" + dt.Year);
    DateTime ld = Convert.ToDateTime(dt.Month + "/" + System.DateTime.DaysInMonth(dt.Year, dt.Month) + "/" + dt.Year);
    Response.Write(fd.DayOfWeek.ToString() + " " + ld.DayOfWeek.ToString());
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 5, 2011 10:59 PM
  • User1888482730 posted

    Here you go, let me know if its what you were looking for:

    private DateTime FirstDayOfMonth(DateTime dt){
                return new DateTime(dt.Year, dt.Month, 1);
            }
    
            private DateTime LastDayOfMonth(DateTime dt) {
                return FirstDayOfMonth(dt).AddMonths(1).AddDays(-1);
            }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 6, 2011 2:38 AM
  • User-1673382244 posted
    In this code we can get current month last date and first date , using C#.Net
    
    DateTime firstDay= new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
    
     int DaysinMonth = DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month)-1;
    
    DateTime lastDay = firstDay.AddDays(DaysinMonth);
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 6, 2011 3:21 AM
  • User3690988 posted

    This is in VB, but it will return the first and last second of the month.

        ' DATEADD(ms, -5, DATEADD(mm, DATEDIFF(m, 0, GETDATE()) + 1, 0))  - Taken from a T-SQL query to find last second of the month
    
        DATEDIF = DateDiff(DateInterval.Month, #1/1/1900#, DateTime.Now)
        firstDay = DateAdd(DateInterval.Month, DATEDIF, #1/1/1900#)
        lastDay = DateAdd(DateInterval.Month, DATEDIF + 1, #1/1/1900#).AddMilliseconds(-1)
        Label1.Text = firstDay.ToString & " - " & lastDay.ToString
    

    The label will print 10/1/2011 12:00:00 AM - 10/31/2011 11:59:59 PM

    Untested, converted c# code:

    DateTime firstDay = default(DateTime);
    DateTime lastDay = default(DateTime);
    long DATEDIF = 0;
    DATEDIF = DateDiff(DateInterval.Month, 1/1/1900 12:00:00 AM, DateTime.Now);
    firstDay = DateAdd(DateInterval.Month, DATEDIF, 1/1/1900 12:00:00 AM);
    lastDay = DateAdd(DateInterval.Month, DATEDIF + 1, 1/1/1900 12:00:00 AM).AddMilliseconds(-1);
    Label1.Text = firstDay.ToString + " - " + lastDay.ToString;
    

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 6, 2011 7:43 AM