Reputation: 511
$('#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
Upvotes: 1
Views: 3357
Reputation: 17481
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">
Upvotes: 3
Reputation: 4162
You could use the moment library and
var date = moment(timeAdded)
Upvotes: 0
Reputation: 4321
use javascript date object
$('#submit').on('click', function(e) {
var $textVal = $('#textVal').val();
var $listItems = $('.listItems');
var timeAdded = new Date;
$listItems.prepend('<li>' + $textVal + ' added at ' + (timeAdded.getDay() + '-'+ (timeAdded.getMonth() + 1) + '-' + timeAdded.getFullYear() + " " + timeAdded.getHours() + ":" + timeAdded.getMinutes() ) + '</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">
Upvotes: 1