Link to home
Start Free TrialLog in
Avatar of tonelm54
tonelm54

asked on

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

SOLUTION
Avatar of numberkruncher
numberkruncher
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Hammo777
Hammo777

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

Avatar of tonelm54

ASKER

Got it working