How do I format .created_at to produce the same time format as my Ruby code?
Javascript / jQuery
<script>
setInterval(function () {
fetch('/pages/markets.json')
.then((response) => response.json())
// .then((data) => console.log(data))
.then((data) => {
$('#container').html('')
data.forEach((item) => {
$('#container').append('<li><div id="created" class="text-xs mb-1">' + item.created_at + '</div><div id="text" >' + item.tweet_text +'</div></li>')
})
});
}, 10000);
</script>
Returns: 2022-08-15T13:55:32.089Z
Ruby on Rails
t.created_at.in_time_zone("Eastern Time (US & Canada)").strftime('%I:%M %p')
Returns: 01:30 PM
I think it's better to send formatted date to frontend from backend
But if you can't, you can use moment.js
moment(item.created_at).tz('America/New_York').format('hh:mm A')
or
moment.tz(item.created_at, 'EST').format('hh:mm A')
Please look:
You can also use vanilla JS Intl.DateTimeFormat
Intl.DateTimeFormat('en', { hour: '2-digit', minute: '2-digit', timeZone: 'EST' }).format(new Date(item.created_at))