javascriptjquerydatetime

event.timeStamp to date


$('#submit').on('click',function(e){
	var $textVal = $('#textVal').val();
	var $listItems = $('.listItems');	
	var timeAdded = e.timeStamp;

	$listItems.prepend('<li>' + $textVal + ' added at ' + timeAdded  + '</li>');


	$('#textVal').val(' ');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
	<ol class="listItems">
		
	</ol>
</div>
<input type="textarea" placeholder="What to do?" id="textVal">
<input type="submit" value="To do!" id="submit">

When a toDo list item is added by pressing the to do! button, to add the item and the date that was added. I tried using the e.timeStamp but its showing the number of milliseconds from january 1st 1970 to when the event was triggered. I tried to convert that but I fail . How should I convert it so I get the exact time and date that the list item was added?

Thank you


Solution

  • event.timeStamp is supposed to equal new Date().getTime(). It's currently not. My advice would be to ignore the event timeStamp and use a common Date object.

    $('#submit').on('click',function(e){
    	var $textVal = $('#textVal').val();
    	var $listItems = $('.listItems');	
        var dateAdded = new Date();
    
    	$listItems.prepend('<li>' + $textVal + ' added at ' + dateAdded  + '</li>');
    
    
    	$('#textVal').val(' ');
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div>
    	<ol class="listItems">
    		
    	</ol>
    </div>
    <input type="textarea" placeholder="What to do?" id="textVal">
    <input type="submit" value="To do!" id="submit">