0

I am trying to insert the date and time (in a manner in which i have gotten to work in console.log). Essentially I have a forum and when the user comments on a post and clicks comment button it uploads that text he or she entered into the comment section. That works fine, BUT I want it to insert the current date and time too. As of now I just have a static date and time.

I tried using: document.write(dateNTime)

var t = new Date()

var DateNTime = (t.toDateString() + ", at " + t.getHours() + ":" + 
t.getMinutes())
console.log(DateNTime)

$('#commentButton').on('click', function() {
if($('#forumCommentSearchBox').val() ) {

  $('.commentList').append('<li> <div class="commenterImage"> \
                            <img 
src="http://lorempixel.com/50/50/people/9"> \
                            </div> \
                            <div class="commentText"> \
                              <p \ 
class="">'+$('#forumCommentSearchBox').val()+'</p> \
                              <span class="date sub-text">on March 5th, 
2014</span> \
                            </div></li>');


}
 else{
  alert("idiot");
}
});

1 Answer 1

0

If I understand you correctly...

var t = new Date()

var DateNTime = (t.toDateString() + ", at " + t.getHours() + ":" + 
t.getMinutes())

$('#commentButton').on('click', function() {
if($('#forumCommentSearchBox').val() ) {

$('.commentList').append('<li> <div class="commenterImage"> \
                            <img src="http://lorempixel.com/50/50/people/9"> \
                            </div> \
                            <div class="commentText"> \
                              <p class="">'+$('#forumCommentSearchBox').val()+'</p> \
                              <span class="date sub-text">on ' + DateNTime + '</span> \
                            </div></li>');

}
 else{
  alert("idiot");
}
});
Sign up to request clarification or add additional context in comments.

3 Comments

var t = new Date(); var DateNTime = ... must be in a function handling the click event, because it takes time pressing, not loading the script.
Oh, oh... now I understand him @stdob--. Let me update real quick.
current date and time, so inside :) @madness

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.