
[英]How do I properly format interpolated ruby objects as jquery selectors in rails, using haml?
[英]How do I produce same time format using Javascript as my Ruby on Rails format does? [duplicate]
如何 format.created_at 以生成與我的 Ruby 代碼相同的時間格式?
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>
返回:2022-08-15T13:55:32.089Z
Ruby on Rails
t.created_at.in_time_zone("Eastern Time (US & Canada)").strftime('%I:%M %p')
返回時間:下午 1:30
我認為最好將格式化的日期從后端發送到前端
但如果你不能,你可以使用 moment.js
moment(item.created_at).tz('America/New_York').format('hh:mm A')
或者
moment.tz(item.created_at, 'EST').format('hh:mm A')
敬請期待:
您還可以使用香草 JS Intl.DateTimeFormat
Intl.DateTimeFormat('en', { hour: '2-digit', minute: '2-digit', timeZone: 'EST' }).format(new Date(item.created_at))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.