這是我現在擁有的代碼:
const moment = require('moment')
const m = moment
const currDay = m().format('D')
const dayOfWeek = m().format('dddd')
const daysInMonth = m().daysInMonth()
const startOfMonth = moment().startOf('month').format('YYYY-MM-DD hh:mm');
const endOfMonth = moment().endOf('month').format('YYYY-MM-DD hh:mm');
我需要創建一個日歷行,其中第一個專案是今天的日期,其余的日歷專案將是根據當前月份剩余的天數,這樣我就可以在我的 HTML 中渲染每一天維。
示例:星期三、9 星期四、10 星期五 ... 31 星期五。
uj5u.com熱心網友回復:
我認為 OP 被過早格式化的常見錯誤絆倒了。format 很高興看到中間結果,但這樣做會產生一個不利于額外計算的字串。
嘗試只處理日期物件。僅當您必須:(a) 呈現給人類讀者,或 (b) 序列化以進行存盤或傳輸時才轉換為字串。
不格式化作業...
const daysRemainingThisMonth = moment().endOf('month').diff(moment(), 'days');
console.log(`There are ${daysRemainingThisMonth} days remaining this month`)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/376755.html
標籤:javascript 数组 Vue.js 日期 瞬间
