2

I have been trying to get the months full name using the script (shown below) but couldn't able to get it.. seems like some thing is missing..

Code as follows:

var d=new Date();
var monthNames = ["January", "February", "March", "April", "May", "June",
                  "July", "August", "September", "October", "November", "December"];
var timeString = d.getDate() + '-' + d.getFullYear()
    +  ' ' + d.getHours() + ':' + d.getMinutes() + ':'
    + d.getSeconds();
document.getElementById("date").innerHTML = (monthNames[d.getMonth()]) + timeString;
<div>
  <small><B><p id="date" name="date" class="date"></p></B></small>
</div>

Any Help is Appreciated..

2

3 Answers 3

1
var d=new Date();
        Date.prototype.getMonthName = function() {
            var monthNames = ["January", "February", "March", "April", "May", "June",
            "July", "August", "September", "October", "November", "December"];
            return monthNames[this.getMonth()];
        }


        var timeString = d.getDate() + '-' + d.getMonthName() + '-' + d.getFullYear()
            +  ' ' + d.getHours() + ':' + d.getMinutes() + ':' 
            + d.getSeconds();
        document.getElementById("date").innerHTML =timeString;

Your code is OK

Sign up to request clarification or add additional context in comments.

Comments

0

There is one simple issue when creating timeString variable. Instead monthNames you should invoke your method "getMonthName" instead just asking for variable you created inside getMonthName.

var timeString = d.getDate() + '-' + d.getMonthName() + '-' + d.getFullYear() +  ' ' + d.getHours() + ':' + d.getMinutes() + ':' + d.getSeconds();

3 Comments

Where is the getMonthName method documented?
This is the purpose of prototype. You created it by yourself when using prototype. Date.prototype.getMonthName = function() .....
The OP has edited out that part of the code. I'm not the OP.
0

Here is a little fiddle that does what you want.

<div>
    <p id="date" name="date" class="date">Date</p>
</div>

<script>
    Date.prototype.getMonthName = function() {
        var monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
        return monthNames[this.getMonth()];
    };

    var d = new Date();

    var monthName = d.getMonthName();
    var timeString = d.getDate() + '-' + monthName + '-' + d.getFullYear() + ' ' + d.getHours() + ':' + d.getMinutes() + ':' + d.getSeconds();
    document.getElementById("date").innerHTML = timeString;

</script>

Notice setting date and time in your paragraph is outside of prototype declaration. Also beware of return statement (code after it won't execute)!

Comments