卖坚果的怪叔叔 卖坚果的怪叔叔
  • 首页
  • 生活
  • 技术
  • 记录
  • 工具箱
  • 关于我
首页 › 技术 › JS生成Van-Picker 指定日期格式

JS生成Van-Picker 指定日期格式

坚果大叔
2023-06-26 17:28:12技术阅读 3,262

项目中需要根据后台配置生成指定的Picker日期格式,按照自然月或者按照指定日期生成月区间。

generateDateRanges(year) {
  const dateRanges = [];
  const currentYear = new Date().getFullYear();
  const currentMonth = new Date().getMonth();

  // 计算结束月份
  const endMonth = (year >= currentYear) ? currentMonth : 11;

  // 将日期格式化为数组对象
  for (let month = 0; month <= endMonth; month++) {
    const startDate = new Date(year, month, 1);
    const endDate = new Date(year, month + 1, 0);
    dateRanges.push({
      label: '${startDate.getMonth() + 1}月${startDate.getDate()}日至${endDate.getMonth() + 1}月${endDate.getDate()}日',
      value: [`${startDate.getFullYear()}${('0' + (startDate.getMonth() + 1)).slice(-2)}${('0' + startDate.getDate()).slice(-2)}', '${endDate.getFullYear()}${('0' + (endDate.getMonth() + 1)).slice(-2)}${('0' + endDate.getDate()).slice(-2)}']
    });
  }
  this.monthObjs = dateRanges
}

输出格式

[
  {
    label: '1月1日至1月31日',
    value: [ '20230101', '20230131' ]
  },
  {
    label: '2月1日至2月28日',
    value: [ '20230201', '20230228' ]
  },
  {
    label: '3月1日至3月31日',
    value: [ '20230301', '20230331' ]
  },
  {
    label: '4月1日至4月30日',
    value: [ '20230401', '20230430' ]
  },
  {
    label: '5月1日至5月31日',
    value: [ '20230501', '20230531' ]
  },
  {
    label: '6月1日至6月30日',
    value: [ '20230601', '20230630' ]
  }
]

实际效果

JS生成Van-Picker 指定日期格式-卖坚果的怪叔叔

customDate(selectYear, date) {
  const now = new Date();

  // 如果当前月份为一月,则需要计算去年的所有月份
  const length = now.getFullYear() === selectYear ? now.getMonth() : 12;
  const startOffset = date;

  // 定义结束日期的偏移量为起始日期偏移量减一
  const endOffset = startOffset - 1;

  // 定义起始月份和结束月份的数组
  const months = Array.from({ length: length }, (_, i) => ({
    start: new Date(i === 0 ? selectYear - 1 : selectYear, i, startOffset),
    end: new Date(i === 11 ? selectYear : selectYear, (i + 1) % 12, endOffset),
  }));

  // 将日期格式化为数组对象
  const monthObjs = months.map(({ start, end }) => ({
    label: '${start.getMonth() + 1}月${start.getDate()}日至${end.getMonth() + 1}月${end.getDate()}日',
    value: [
      '${start.getFullYear()}${(start.getMonth() + 1).toString().padStart(2, '0')}${start.getDate().toString().padStart(2, '0')}',
      '${end.getFullYear()}${(end.getMonth() + 1).toString().padStart(2, '0')}${end.getDate().toString().padStart(2, '0')}'
    ],
    isStart: end > now,
  }));

  this.monthObjs = monthObjs
}

输出结果

[
  {
    "label": "1月6日-2月5日",
    "value": ["20230106", "20230205"],
  },
  {
    "label": "2月6日-3月5日",
    "value": ["20230206", "20230305"],
  },
  {
    "label": "3月6日-4月5日",
    "value": ["20230306", "20230405"],
  },
  {
    "label": "4月6日-5月5日",
    "value": ["20230406", "20230505"],
  },
  {
    "label": "5月6日-6月5日",
    "value": ["20230506", "20230605"],
  }
]

实际效果

JS生成Van-Picker 指定日期格式-卖坚果的怪叔叔

日期
赞赏 赞(1)
夏天到了
上一篇
Md5引发的血案
下一篇
在小程序中查看

这是个😊,挣点服务器费用😜

热门文章
日常,下班啊,唉唉唉唉
在博客中加上memos记录展示。
好险!我差点被诈骗了~
前端两三问(20190915)
失业在家,我给小朋友做了一个练字小程序!
秋末的晚上
专题标签
agent (1) CSS (15) dom (1) Es6入门系列 (14) Git (3) html (1) iview (1) JavaScript (9) js (4) Lif (1) Node (4) pyecharts (1) Python (10) ref (1) scriptable (1) Skill (1) vsCode (1) vue (4) wordpress (2) 三月 (1) 上海 (1) 假期 (1) 其他分享 (3) 前端两三问 (15) 古镇 (1) 外滩 (1) 对象 (1) 小程序 (5) 年度总结 (6) 年终总结 (1) 微信机器人 (4) 打字机 (1) 技术 (1) 日常问题 (8) 日常问题记录 (1) 春天 (2) 服务器 (2) 海族馆 (1) 生活 (8) 网上国网 (1) 记录 (3) 通勤 (1) 重学JavaScript (11) 面试题 (10) 🌸 (1)
什么是Agent?
2026-08-15 23:49:12
507 0 2
什么是 Skill?
2026-08-12 21:32:42
436 1 0
失业在家,我给小朋友做了一个练字小程序!
2026-08-07 16:21:52
864 10 1
上线8年,15万用户的小程序,一共赚了多少钱?
2026-05-30 18:01:24
4,827 4 0
1
  • 1
博主

一位佛系的前端开发者,略通摄影,乐于尝试新事物,热衷于美食。

友链
故事胶片
公众号
坚果大叔 执行上下文 卖坚果的怪叔叔 Dacking
Copyright © 2017-2026 卖坚果的怪叔叔

开往-友链接力

萌ICP备20230818号

苏ICP备18048410号-3
  • 首页
  • 生活
  • 技术
  • 记录
  • 工具箱
  • 关于我
# CSS # # JavaScript # # vue # # 微信 # # 生活 #
坚果大叔
345
文章
171
评论
432
喜欢