How to i assign current date in dd-mm-yyyy format in html textbox in asp.net page

How do i assign the Current Date in dd-mm-yyyy format in aspx page in HTML Textbox
searchsanjaysharmaAsked:
Who is Participating?
 
Amandeep Singh BhullarConnect With a Mentor Commented:
Attached is the simple function that will return current date in dd-mm-yyyy format.

Hope this will help you in solving your issue
<script type="text/javascript">
    function getCurrentDate() {
        var date = new Date();
        var d = date.getDate();
        var day = (d < 10) ? '0' + d : d;
        var m = date.getMonth() + 1;
        var month = (m < 10) ? '0' + m : m;
        var yy = date.getYear();
        var year = (yy < 1000) ? yy + 1900 : yy;
        var FinalDate = day + "-" + month + "-" + year;
        return FinalDate;
    }
 </script>

Open in new window

0
 
Michel PlungjanIT ExpertCommented:
And in the 21st century we change line 8 and 9 to var year = date.getFullYear();
0
 
Amandeep Singh BhullarConnect With a Mentor Commented:
@mplungjan: Thanks for updating me.

I have attached the modified code
<script type="text/javascript">
    function getCurrentDate() {
        var date = new Date();
        var d = date.getDate();
        var day = (d < 10) ? '0' + d : d;
        var m = date.getMonth() + 1;
        var month = (m < 10) ? '0' + m : m;
        var yy = date.getFullYear();
        var FinalDate = day + "-" + month + "-" + yy;
        return FinalDate;
    }
 </script>

Open in new window

0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
Michel PlungjanConnect With a Mentor IT ExpertCommented:
Reuse some vars, no need for a ternary here and return the string too...

<script type="text/javascript">
  function getCurrentDate() {
    var date = new Date();
    var d = date.getDate();
    if (d < 10) d='0'+d;
    var m = date.getMonth() + 1;
    if (m < 10) m='0'+m;
    return ""+ day + "-" + month + "-" + date.getFullYear();
  }
  window.onload=function() {
    document.forms[0].dateField.value=getCurrentDate();
  }
 </script>

Open in new window

0
 
Ryan ChongConnect With a Mentor Commented:
since it's an ASP.NET page, you can try ASP.NET method, like in form load:

protected void Page_Load(object sender, EventArgs e)
    {
        YourTextBox.Text = DateTime.Today.ToString("dd-MM-yyyy");
    }
0
 
Michel PlungjanConnect With a Mentor IT ExpertCommented:
NOTE: that will be server time, not visitor time
0
 
Ryan ChongConnect With a Mentor Commented:
I don't think questioner mentioned clearly he needs either server time or visitor time, unless there's a timezone issue, else both should returns the same.
0
 
Michel PlungjanConnect With a Mentor IT ExpertCommented:
I know - hence the "NOTE:" and not "WARNING" or something;)
0
 
Ryan ChongConnect With a Mentor Commented:
; )
0
 
searchsanjaysharmaAuthor Commented:
ok
0
All Courses

From novice to tech pro — start learning today.