Javascript

前端js月视图日期自动补全 js comon方法

本文主要是介绍前端js月视图日期自动补全 js comon方法,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

前端js月视图日期自动补全
https://blog.csdn.net/u013262823/article/details/90406953

JS日期自动补全

js日期自动补全
实现一个能够实现日期自动补全的前端日历。主要计算数据如下:(代码中dateFormat时间格式转换方法将在下一篇文章贴出)
1、知道今天的日期(today)

const today = dateFormat().today()
1

2、一个月有多少天 (daysInMonth)

const daysInMonth = (date) => {
    if(date.indexOf('-') === date.lastIndexOf('-')) {
        date = `${date}-01`
    }
    const d = new Date(`${date.replace(/-/g, '/')}`)
    d.setMonth(d.getMonth()+1)
    d.setDate(0)
    return d.getDate()
}
123456789

3、当前月的第一天和最后一天是周几 (firstAndLastDay)

const firstAndLastDay = function (date) {
    let todayWeek = dateFormat(date).day(),
        todayDate = dateFormat(date).today(),
        first = todayWeek - (todayDate - 1)%7,
        last = (todayWeek + (daysInMonth(date) - todayDate)%7)%7
    return {first, last}
}
1234567

4、上个月最后一天(beforeMonthLastDay)

const beforeMonthLastDay = function (date) {
    return daysInMonth(dateFormat(date).add(-1, 'month').format('YYYY-MM-DD'))
}
123

5、下一个月是几月份(lastMonthMonth)

const lastMonthMonth = function (date) {
    return dateFormat(date).add(1, 'month').month() + 1
}
123

6、补齐月视图面板天数(fillMonthPanel)

fillMonthPanel的实现方法如下

let _daysInMonth = daysInMonth(date),
        _firstAndLastDay = firstAndLastDay(date),
        _beforeMonthLastDay = beforeMonthLastDay(date),
        thisMonth = Number(date.split('-')[1]),
        _lastMonthMonth = lastMonthMonth(date),
        beforeLength = _firstAndLastDay.first,
        afterLength = 6 - _firstAndLastDay.last,
        beforeDates = [],
        afterDates = [],
        days = []
    for (let i = _beforeMonthLastDay - beforeLength + 1; i <= _beforeMonthLastDay; i++) {
    beforeDates<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span><span class="token punctuation">{<!-- --></span>
        day<span class="token punctuation">:</span> i<span class="token punctuation">,</span>
        type<span class="token punctuation">:</span> <span class="token string">'before'</span><span class="token punctuation">,</span>
        nickDay<span class="token punctuation">:</span> i
<span class="token punctuation">}</span>
<span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">let</span> i <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> i <span class="token operator">&lt;=</span> _daysInMonth<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span>
    days<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span><span class="token punctuation">{<!-- --></span>
        day<span class="token punctuation">:</span> i<span class="token punctuation">,</span>
        type<span class="token punctuation">:</span> <span class="token string">'normal'</span><span class="token punctuation">,</span>
        nickDay<span class="token punctuation">:</span> i
<span class="token punctuation">}</span>
<span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">let</span> i <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> i <span class="token operator">&lt;=</span> afterLength<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span>
    afterDates<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span><span class="token punctuation">{<!-- --></span>
        day<span class="token punctuation">:</span> i<span class="token punctuation">,</span>
        type<span class="token punctuation">:</span> <span class="token string">'after'</span><span class="token punctuation">,</span>
        nickDay<span class="token punctuation">:</span>  i
<span class="token punctuation">}</span>
<span class="token keyword">return</span> beforeDates<span class="token punctuation">.</span><span class="token function">concat</span><span class="token punctuation">(</span>days<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">concat</span><span class="token punctuation">(</span>afterDates<span class="token punctuation">)</span>

123456789101112131415161718192021222324252627282930

这也就能实现出一个自动补全月视图面板的前端日历。

这篇关于前端js月视图日期自动补全 js comon方法的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!