Im working on a messaging app and i cant figure out how to capture time when SEND/Enter button was clicked and display it next to the message
Vanilla JS
Thank you!
CodePudding user response:
So are you trying to display the time the button was clicked, and display it to the sender / recipient in their local time?
If so, you should just be able to use the Date constructor like so:
const date = new Date();
That will give you the UTC time. To convert to local time, you can use the .toLocaleTimeString()
method. This would log your local time in US format:
const date = new Date();
console.log(date.toLocaleTimeString('en-US'));
So what you could do is have an event listener on the button, and when the button is clicked, create a new date, and convert to local time.
const button = document.querySelector('button');
const par = document.querySelector('p')
button.addEventListener('click', () => {
const date = new Date();
const local = date.toLocaleTimeString('en-US');
par.innerHTML = local
})
<button>
Click me
</button>
<p>
</p>
JSFiddle: https://jsfiddle.net/f198wvdt/2/
Is this along the lines of what you're looking for?