Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how to find first day and last day in given month,,,

ex: i will enter march month , to get the first day of march 1-3-2014 , last day 31-3-2014

reply
Posted
Updated 29-May-14 19:40pm
v2

Hello,
there are many examples available in google.First see the DateTime structure from this link
DateTime Structure.
Then Try this
var today = DateTime.Today;
var month = new DateTime(today.Year, month , 1); //Put month as 3 if it is March      
var firstday = month.AddMonths(-1);
var lastday = month.AddDays(-1);

thanks
 
Share this answer
 
Firstday:
C#
DateTime FD= new DateTime(2014, 3, 1);

LastDay:
C#
DateTime LD= FD.AddMonths(1).AddDays(-1);
 
Share this answer
 
v2
chk thi link :

http://blog.sqlauthority.com/2007/05/13/sql-server-query-to-find-first-and-last-day-of-current-month/[^]


SQL
DECLARE @mydate DATETIME
SELECT @mydate = GETDATE()
SELECT CONVERT(VARCHAR(25),DATEADD(dd,-(DAY(@mydate)),@mydate),101) ,
'Last Day of Previous Month'
UNION
SELECT CONVERT(VARCHAR(25),DATEADD(dd,-(DAY(@mydate)-1),@mydate),101) AS Date_Value,
'First Day of Current Month' AS Date_Type
UNION
SELECT CONVERT(VARCHAR(25),@mydate,101) AS Date_Value, 'Today' AS Date_Type
UNION
SELECT CONVERT(VARCHAR(25),DATEADD(dd,-(DAY(DATEADD(mm,1,@mydate))),DATEADD(mm,1,@mydate)),101) ,
'Last Day of Current Month'
UNION
SELECT CONVERT(VARCHAR(25),DATEADD(dd,-(DAY(DATEADD(mm,1,@mydate))-1),DATEADD(mm,1,@mydate)),101) ,
'First Day of Next Month'
 
Share this answer
 
Comments
Animesh Datta 30-May-14 2:15am    
here the question is tagged for C#.Net . and you gave the answer in SQl
VB
--First day of last month
SELECT DATEADD(m,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE()), 0))
--Last day of last month
SELECT DATEADD(d,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE()),0))


Please accept the answer if its work for you...
 
Share this answer
 
Try this

C#
DateTime dt = DateTime.Now;
        int year = dt.Year;

        string firstDay = "";
        string lastDay = "";


        string input = "march";

        for (int i = 1; i <= 12; i++)
        {
            string month = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(i);
            if (month.ToLower() == input.ToLower())
            {
                DateTime dtFirstDay = new DateTime(year, i, 1);
                DateTime dtLastDay = new DateTime(year, i + 1, 1).AddDays(-1);
                firstDay = dtFirstDay.ToString("dd-MM-yyyy");
                lastDay = dtLastDay.ToString("dd-MM-yyyy");
            }
        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900