Get First Date And Last Date Of User Passed Date

RAVI 1,056 Reputation points
2022-07-07T07:58:23.86+00:00

Hello

In asp.net C# code behind

If i enter for example like this in textbox1 15-May-2022 then In textbox2 it should show month first date 01-May-2022 and In textbox3
last date of that month 15-May-2022

If i enter for example like this in textbox1 04-JUN-2022 then In textbox2 it should show month first date 01-Jun-2022 and In textbox3
last date of that month 04-Jun-2022

If i enter for example like this in textbox1 07-JUL-2022 then In textbox2 it should show month first date 01-JUL-2022 and In textbox3
last date of that month 07-JUL-2022

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,494 questions
0 comments No comments
{count} votes

Accepted answer
  1. Bruce (SqlWork.com) 65,576 Reputation points
    2022-07-07T17:25:24.01+00:00

    what part do you need help with? as using C# would require a form post or Ajax, I'd just do it in javascript:

     date:<input id="text1" onchange="calcDates()"><br>   
      first:<input id="text2"><br>   
      last:<input id="text3"><br>   
         
    <script>   
    function formatDate(d) {   
      return (d.getMonth() + 1) + "/" + d.getDate() + "/" + d.getFullYear();   
    }     
    function calcDates(){   
        var d = new Date(document.getElementById('text1').value);   
        if (!isNaN(d)) {   
          var firstDay = new Date(d.getFullYear(), d.getMonth(), 1);   
          var lastDay = new Date(d.getFullYear(), d.getMonth() + 1, 0);   
       
          document.getElementById('text2').value = formatDate(firstDay);   
          document.getElementById('text3').value = formatDate(lastDay);   
        }   
    }   
    </script>   
       
    
    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Yijing Sun-MSFT 7,081 Reputation points
    2022-07-08T02:34:03.517+00:00

    Hi @RAVI ,
    I think you could use datetime.Just like this:

    DateTime v1 = Convert.ToDateTime(TextBox1.Text);  
    DateTime s1 = new DateTime(v1.Year, v1.Month, 1);  
    TextBox2.Text = s1.ToString();  
    TextBox3.Text = TextBox1.Text;  
    

    Best regards,
    Yijing Sun


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  2. Bruce (SqlWork.com) 65,576 Reputation points
    2022-07-11T17:39:03.193+00:00

    I did not notice the date format. here is a simple one for the desired format:

    function formatDate(d) {   
       var months = "Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec".split(",");  
       return ("0"+ d.getDate()).substring(-2) + "-" + months[d.getMonth()] + "-" + d.getFullYear();   
     }    
    
    0 comments No comments

  3. madhusudhan suddala 1 Reputation point
    2022-07-13T02:27:54.483+00:00

    I think you could use below two simple functions.

    static string getMonthStartDate(string strDate)
    {
    DateTime dtresult = Convert.ToDateTime(strDate);
    return "01"+ strDate.Substring(2);
    }
    static string getMonthEndDate(string strDate)
    {
    DateTime dtresult = Convert.ToDateTime(strDate);
    return Convert.ToString(DateTime.DaysInMonth(dtresult.Year,dtresult.Month)) + strDate.Substring(2);
    }

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.