1

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("");       
    });
AdiT
  • 539
  • 5
  • 18

3 Answers3

4

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.

Sam Hanley
  • 4,707
  • 7
  • 35
  • 63
  • I don't know - I saw that someone suggested in a comment that you might be getting the month incorrectly, so I just thought I'd note that I didn't proofread your code for any other issues. I didn't mean to imply that I saw anything else wrong with it, just that I didn't verify anything beyond that this fixes the specific issue you asked for help with. That said -- since you asked, the only issue I see is that the month is zero-based, as mentioned. The rest looks fine by my eye. – Sam Hanley Apr 29 '14 at 13:20
1

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);
Irvin Dominin
  • 30,819
  • 9
  • 77
  • 111
  • How is it possible to get the Month in this format: "Apr" instead of "04" ?? – AdiT Apr 29 '14 at 13:34
  • 1
    @AdiT you can use a function to calculate and display it, see: http://jsfiddle.net/9jsjw/1/ – Irvin Dominin Apr 29 '14 at 13:36
  • Wrapping that string in a div is an unnecessary hack to make a misuse of the jQuery selector work right. The poster should just be creating a string and then appending it using div.append(), rather than adding unnecessary cluttering elements to the DOM. – Sam Hanley Apr 29 '14 at 13:51
  • @sphanley is not a hack, is a way of doing it: http://stackoverflow.com/questions/1443233/easier-way-to-get-a-jquery-object-from-appended-element I don't think it deserves a downvote... – Irvin Dominin Apr 29 '14 at 13:54
  • Eh, maybe a downvote is overkill but the difference is that in the linked question, the poster wanted to append a div. In yours, you're adding a div that serves no other function then to turn a string into an element within a div, when they ought to just be appending a text node. – Sam Hanley Apr 29 '14 at 13:56
0

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/

Ashok Damani
  • 3,896
  • 4
  • 30
  • 48