0

Im trying to print current date on page as time stamp when a new task(obj) is displayed, but new time stamps are all the same date regardless of when the time stamp is made. Any assistance would be greatly appreciated.

 function getDate() {
     var today = new Date();
     var dd = today.getDate();
     var mm = today.getMonth() + 1;
     var yyyy = today.getFullYear();
     var time = today.getTime();
     if (dd < 10) {
         dd = '0' + dd
     }

     if (mm < 10) {
         mm = '0' + mm
     }

     today = mm + '/' + dd + '/' + yyyy + ' ' + time;

     localStorage.setItem('newDate', today);
     var newDate = localStorage.getItem('newDate');
     return (newDate);
 }

 function show() {
     var todos = get_todos();
     var date = localStorage.getItem('newDate');
     var html = '<table>';
     for (var i = 0; i < todos.length; i++) {
         html += '<br><tr><strong>' +
             '<input type="image" src="Pictures/remove.png"   class="remove" id="' + i +
             '"></input>' + todos[i] + ' - ' + date + '</strong><input type="checkbox"    name="cBox" id="isDone"><label for="cBox"</label></tr><br>';
     };
     html += '</table>';


     document.getElementById('todos').innerHTML = html;

     var buttons = document.getElementsByClassName('remove');
     for (var i = 0; i < buttons.length; i++) {
         buttons[i].addEventListener('click', remove);
     };
 }

 document.getElementById('add').addEventListener('click', add);
 show();
nnnnnn
  • 147,572
  • 30
  • 200
  • 241
Kris
  • 33
  • 1
  • 7
  • 2
    Where do you call getDate()? As that is how your localStorage seems to be set. – JonSG Jul 11 '16 at 02:30
  • 1
    Why are you using localStorage for this at all? Also, please learn to [indent your code](http://jorendorff.blogspot.com.au/2012/01/why-do-we-indent-code.html). – nnnnnn Jul 11 '16 at 02:51
  • You don't need parentheses around the variable you are returning. Also there isn't much of a point pulling the value out of localStorage into `newDate` to return it. You could just return `today`, it will be the exact same value. Adding a read from localStorage just slows things down. – Useless Code Jul 11 '16 at 06:59
  • Sorry i am in JavaScript course, this is one of my first applications with js. still very new to it. I have to use local storage for the date as it is apart of my assignment. its a todo web page. the list needs a time stamp for each item or todo task. When u return to the page or the page is refreshed the time stamp needs to remain along with a check box saying if it ha been completed. – Kris Jul 12 '16 at 21:51

1 Answers1

1
var date = localStorage.getItem('newDate');

I think should be

var date = getDate();
nnnnnn
  • 147,572
  • 30
  • 200
  • 241