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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Amandeep Singh BhullarCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Michel PlungjanIT ExpertCommented:
And in the 21st century we change line 8 and 9 to var year = date.getFullYear();
0
Amandeep Singh BhullarCommented:
@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
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

Michel PlungjanIT 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 ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
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 PlungjanIT ExpertCommented:
NOTE: that will be server time, not visitor time
0
Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
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 PlungjanIT ExpertCommented:
I know - hence the "NOTE:" and not "WARNING" or something;)
0
Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
; )
0
searchsanjaysharmaAuthor Commented:
ok
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Scripting Languages

From novice to tech pro — start learning today.