Domanda

When I add this part of code to the script it doesn't display the Date and time:

    var d = new Date();
            var $fulaDate = $(d.getFullYear() + "-" + d.getMonth() 
            + "-" + d.getDate() + "T" + d.getHours() + ":" + d.getMinutes() 
            + ":" + d.getSeconds()).appendTo(div);

I Can't figure out what the problem is.

Live Demo

JQuery

function addComment(name1) {

        var container = $('#divComments');
        var inputs = container.find('label');
        var id = inputs.length + 1;

        var div = $('<div />', { class: 'CommentStyle' });

        $('<label />', {
            id: 'comment' + id,
            text: name1
        }).appendTo(div);

     var d = new Date();
     var $fulaDate = $(d.getFullYear() + "-" + d.getMonth() + "-" + d.getDate() + "T" +     d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds()).appendTo(div);

        div.appendTo(container); 

    }

    $('#submit').click(function () {
        addComment($('#comments').val());
        $('#comments').val("");       
    });
È stato utile?

Soluzione 2

You are not building any HTML element, try to enclose your text in a div instead.

Code:

 var $fulaDate = $('<div>'+d.getFullYear() + "-" + d.getMonth() + "-" + d.getDate() + "T" + d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds()+'</div>').appendTo(div);

Demo: http://jsfiddle.net/9jsjw/

UPDATE

If you want to display the month name use an array of month and get its description using the month index.

Code:

var monthNames = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];

var $fulaDate = $('<div>'+d.getFullYear() + "-" + monthNames[d.getMonth()] + "-" + d.getDate() + "T" + d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds()+'</div>').appendTo(div);

Altri suggerimenti

The problem is that you've got your date strings wrapped in the jQuery selector, so instead of being treated as a string, it's looking for an element that matches that string. Here's a fiddle that works, by saving that date info as a var, and then doing div.append($fullDate) rather than using .appendTo(div).

http://jsfiddle.net/LCA72/3/

There may be other bugs in your code, I didn't dig into anything beyond the question of it displaying... but it now displays the value as you're expecting.

replace those 3 lines with:

var d = new Date();
var $fulaDate = d.getFullYear() + "-" + d.getMonth() + "-" + d.getDate() + "T" + d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds();
div.html($fulaDate);

Demo: http://jsfiddle.net/LCA72/6/

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top