locked
Setting Current date in asp.net text field using jQuery - mm/dd/yyyy format RRS feed

  • Question

  • User-73514677 posted

    Hi,

    I am using asp.net textbox to which I want to set the current date using jQuery ( mm/dd/yyyy ) format.

    How to achieve this?

    Thanks

    Friday, September 2, 2016 2:16 PM

Answers

  • User-1142886626 posted

    Hi venkatzeus,

    Code below is for your reference.

    <script type="text/javascript">
        $(document).ready(function () {
            var d = new Date();
            var month = d.getMonth() + 1;
            var day = d.getDate();
              var output =(('' + month).length < 2 ? '0' : '') + month + '/' +
                (('' + day).length < 2 ? '0' : '') + day+ '/'+d.getFullYear()  ;
            $("#Label1").text(output);
        });
    </script>
    <body>
        <form id="form1" runat="server">
            <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
        </form>
    </body>
    

    Best Regards,

    Ailleen

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 5, 2016 10:28 AM
  • User2103319870 posted

    venkatzeus

    That works and shows as 9/5/2016 . Is it possible to show as 09/05/2016

    You can use the Jquery datepicker to assign and format the datevalues as per your need

    Sample Code

    <html>
    <head>
        <link href="https://code.jquery.com/ui/1.12.0/themes/smoothness/jquery-ui.css" rel="stylesheet" type="text/css" />
        <script src="https://code.jquery.com/jquery-3.1.0.js"></script>
        <script src="https://code.jquery.com/ui/1.12.0/jquery-ui.js"></script>
        <script>
            $(document).ready(function () {
                $("#TextBox1").val($.datepicker.formatDate("mm/dd/yy", new Date()));
            });
        </script>
        <body>
            <form runat="server">
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            </form>
        </body>
    </html>
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 5, 2016 1:32 PM

All replies

  • User1213248666 posted

    Hi Venkat,

    Some sample code to set the current date in jquery

    <script>
        $(document).ready(function () {
            var d = new Date(),
                date = ((d.getMonth()+1) + '/' + (d.getDate()) + '/' + d.getFullYear());
            $('#txtDate').val(date);       
        });
    </script>
    Saturday, September 3, 2016 7:26 AM
  • User-73514677 posted

    Hi,

    Thanks for the reply.

    That works and shows as 9/5/2016 . Is it possible to show as 09/05/2016

    Thanks

    Monday, September 5, 2016 4:48 AM
  • User-1142886626 posted

    Hi venkatzeus,

    Code below is for your reference.

    <script type="text/javascript">
        $(document).ready(function () {
            var d = new Date();
            var month = d.getMonth() + 1;
            var day = d.getDate();
              var output =(('' + month).length < 2 ? '0' : '') + month + '/' +
                (('' + day).length < 2 ? '0' : '') + day+ '/'+d.getFullYear()  ;
            $("#Label1").text(output);
        });
    </script>
    <body>
        <form id="form1" runat="server">
            <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
        </form>
    </body>
    

    Best Regards,

    Ailleen

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 5, 2016 10:28 AM
  • User2103319870 posted

    venkatzeus

    That works and shows as 9/5/2016 . Is it possible to show as 09/05/2016

    You can use the Jquery datepicker to assign and format the datevalues as per your need

    Sample Code

    <html>
    <head>
        <link href="https://code.jquery.com/ui/1.12.0/themes/smoothness/jquery-ui.css" rel="stylesheet" type="text/css" />
        <script src="https://code.jquery.com/jquery-3.1.0.js"></script>
        <script src="https://code.jquery.com/ui/1.12.0/jquery-ui.js"></script>
        <script>
            $(document).ready(function () {
                $("#TextBox1").val($.datepicker.formatDate("mm/dd/yy", new Date()));
            });
        </script>
        <body>
            <form runat="server">
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            </form>
        </body>
    </html>
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 5, 2016 1:32 PM
  • User445449386 posted

    Try this

    HTML

    <link href="http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css" rel="stylesheet" />
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
    <input type="text" id="mydate" />

    JavaScript

    $("#mydate").datepicker().datepicker("setDate", new Date());

    Tuesday, September 6, 2016 5:36 AM
  • User1213248666 posted

    Hi Venkat,

    Try this code to get 09/05/2016 format.

    <script>
        $(document).ready(function () {
            var today = new Date();
            var d = today.getDate();
            var m = today.getMonth() + 1;
            var y = today.getFullYear();
            // add a zero in front of numbers<10
            m = checkDate(m);
            d = checkDate(d);
            $('#txtDate').val(m + '/' + d + '/' + y);
        });
        function checkDate(i) {
            if (i < 10) {
                i = "0" + i;
            }
            return i;
        }
     
     
    </script>
    Tuesday, September 6, 2016 7:14 AM