Javascript date and time

Ive been looking for an easy way to put todays date and time into a textbox, but I cant seem to find an easy way of doing it.

Ive started looking at it using the attached code, but it just puts the entire code into the textbox.

So after it runs, the textbox value is:-
function toString() {    [native code]}-0function toString() {    [native code]}-0function toString() {    [native code]} 0function toString() {    [native code]}:0function toString() {    [native code]}:0function toString() {    [native code]}

<body>
                <script type="text/javascript">
                    function putDate(txtTextBox) {
                        var d = new Date();
                        //  

                        var vYear = d.getYear().toString;
                        var vMonth = ((d.getMonth().toString.length == 1) ? "0" + d.getMonth().toString : d.getMonth().toString);
                        var vDay = ((d.getDay().toString.length == 1) ? "0" + d.getDay().toString : d.getDay().toString);

                        var vHour = ((d.getHours().toString.length == 1) ? "0" + d.getHours().toString : d.getHours().toString);
                        var vMin = ((d.getMinutes().toString.length == 1) ? "0" + d.getMinutes().toString : d.getMinutes().toString);
                        var vSec = ((d.getSeconds().toString.length == 1) ? "0" + d.getSeconds().toString : d.getSeconds().toString);

                        txtTextBox.value = vYear + "-" + vMonth + "-" + vDay + " " + vHour + ":" + vMin + ":" + vSec;
                    }
                </script>
<input type="text" onfocus="putDate(this);" />
</body>

Open in new window

tonelm54Asked:
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.

numberkruncherCommented:
What about this from Rick's excellent blog?

http://www.west-wind.com/weblog/posts/282495.aspx
tonelm54Author Commented:
I actually found the solution as:-

    function putDate(txtTextBox) {
        var d = new Date();
       
        var vYear = d.getYear().toString();
        var vMonth = d.getMonth().toString(); if (vMonth.length == 1) { vMonth = "0" + vMonth; }
        var vDay = d.getDate().toString(); if (vDay.length == 1) { vDay = "0" + vDay; }

        var vHour = d.getHours().toString(); if (vHour.length == 1) { vHour = "0" + vDHour; }
        var vMin = d.getMinutes().toString(); if (vMin.length == 1) { vMin = "0" + vMin; }
        var vSec = d.getSeconds().toString(); if (vSec.length == 1) { vSec = "0" + vSec; }

        txtTextBox.value = vYear + "-" + vMonth + "-" + vDay + " " + vHour + ":" + vMin + ":" + vSec;
    }

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
Hammo777Commented:
You just need parenthesis after toString() (all of them):


<body>
                <script type="text/javascript">
                    function putDate(txtTextBox) {
                        var d = new Date();
                        //  

                        var vYear = d.getYear().toString();
                        var vMonth = ((d.getMonth().toString().length == 1) ? "0" + d.getMonth().toString() : d.getMonth().toString());
                        var vDay = ((d.getDay().toString().length == 1) ? "0" + d.getDay().toString() : d.getDay().toString());

                        var vHour = ((d.getHours().toString().length == 1) ? "0" + d.getHours().toString() : d.getHours().toString());
                        var vMin = ((d.getMinutes().toString().length == 1) ? "0" + d.getMinutes().toString() : d.getMinutes().toString());
                        var vSec = ((d.getSeconds().toString().length == 1) ? "0" + d.getSeconds().toString() : d.getSeconds().toString());

                        txtTextBox.value = vYear + "-" + vMonth + "-" + vDay + " " + vHour + ":" + vMin + ":" + vSec;
                    }
                </script>
<input type="text" onfocus="putDate(this);" />
</body>

Open in new window

tonelm54Author Commented:
Got it working
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
JavaScript

From novice to tech pro — start learning today.