2 回答

TA貢獻(xiàn)1797條經(jīng)驗(yàn) 獲得超6個(gè)贊
我認(rèn)為您可以像這樣更改代碼:
$this.text(date.format('{{ "YYYY年M月D日 (dd)"|json_encode()|raw }}'))

TA貢獻(xiàn)1786條經(jīng)驗(yàn) 獲得超13個(gè)贊
ja如https://momentjs.com/docs/#/i18n/ 中所示,從 cdn(或從 npm/yarm 安裝包)導(dǎo)入 moment語(yǔ)言環(huán)境
如果您使用 cdn 托管的 js,則使用以下代碼:
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js" integrity="sha256-4iQZ6BVL4qNKlQ27TExEhBN1HFPvAvAMbFavKKosSWQ=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/locale/ja.js" integrity="sha256-CFWtR1hGN/5Vc+kcJkqeMFND0g6gFFZdnSqUtdL7WOQ=" crossorigin="anonymous"></script>
然后而不是:
$this.text(date.format('{{ 'YYYY年M月D日 (dd)'|raw }}'))
用:
var date = moment(text, 'YYYY/MM/DD');
$this.html(date.locale('ja').format('LL (dd)'))
并刪除:
moment.locale('ja');
線
所以你的腳本將是:
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js" integrity="sha256-4iQZ6BVL4qNKlQ27TExEhBN1HFPvAvAMbFavKKosSWQ=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/locale/ja.js" integrity="sha256-CFWtR1hGN/5Vc+kcJkqeMFND0g6gFFZdnSqUtdL7WOQ=" crossorigin="anonymous"></script>
<script>
let updateArrivalDate = function() {
$('.arrival-date').each(function() {
let $this = $(this);
$selectDate = $this.next().next().find('select').eq(0);
$selectTime = $this.next().next().find('select').eq(1);
let text = $selectDate.val();
let date = moment(text, 'YYYY/MM/DD');
if (date.isValid()) {
$this.text(date.locale('ja').format('LL (dd)'))
}
});
};
$(document).ready(function() {
updateArrivalDate();
$('select').on('change', function() {
updateArrivalDate();
});
});
</script>
請(qǐng)參閱下面的示例,其中僅包含有關(guān)語(yǔ)言的格式:
$(document).ready(function(){
$("#date").html(moment().locale('ja').format('LL (dd)'))
});
<script src="https://momentjs.com/downloads/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/locale/ja.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="date"></div>
所以你讓時(shí)間格式化日期并避免使用 js/twig 的任何復(fù)雜性。保持簡(jiǎn)單,讓 js 完成它的工作,而不是將它們混合在一起。
添加回答
舉報(bào)