js通过开始时间和结束时间计算出中间的所有日期

  • 写在前面
  • 一 已知开始时间和结束时间 计算出中间的所有日期
  • 二 最近七天 一个月 三个月 一年
  • 三 中国标准时间/ yyyy-MM-dd/ yyyy-MM-dd HH:mm:ss转时间戳
  • 四 yyyy-MM-dd/yyyy-MM-dd HH:mm:ss转中国标准时间

写在前面

实际开发过程中,我们有许许多多的产品都设计到了时间,也有时候需要自己转化时间格式,这里推荐一个日期时间处理类库momentjs,但很多时候,我们仅仅是转化个别时间,还没必要安装这么一个依赖;这篇文章将在未来持续更新,搜罗各种关于时间转化的方法

一 已知开始时间和结束时间 计算出中间的所有日期

// 中国标准时间format yyyy-mm-dd
const format = (time) => {let ymd = ''let mouth = (time.getMonth() + 1) >= 10 ? (time.getMonth() + 1) : ('0' + (time.getMonth() + 1))let day = time.getDate() >= 10 ? time.getDate() : ('0' + time.getDate())ymd += time.getFullYear() + '-' // 获取年份。ymd += mouth + '-' // 获取月份。ymd += day // 获取日。return ymd // 返回日期。
}export const getAllDate = (start, end) => {let dateArr = []let startArr = start.split('-')let endArr = end.split('-')let db = new Date()db.setUTCFullYear(startArr[0], startArr[1] - 1, startArr[2])let de = new Date()de.setUTCFullYear(endArr[0], endArr[1] - 1, endArr[2])let unixDb = db.getTime()let unixDe = de.getTime()let stampconst oneDay = 24 * 60 * 60 * 1000;for (stamp = unixDb; stamp <= unixDe;) {dateArr.push(format(new Date(parseInt(stamp))))stamp = stamp + oneDay}return dateArr
}...
// 使用
console.log(getAllDate('2018-12-12', '2019-3-3'))

结果如下:

二 最近七天 一个月 三个月 一年

最近七天:

// 中国标准时间format yyyy-mm-dd
const format = (time) => {let ymd = ''let mouth = (time.getMonth() + 1) >= 10 ? (time.getMonth() + 1) : ('0' + (time.getMonth() + 1))let day = time.getDate() >= 10 ? time.getDate() : ('0' + time.getDate())ymd += time.getFullYear() + '-' // 获取年份。ymd += mouth + '-' // 获取月份。ymd += day // 获取日。return ymd // 返回日期。
}export const getWeekDate = () => {let myDate = new Date()// 获取前一周时间const oneDay = 24 * 60 * 60 * 1000;let oneweekdate = new Date(myDate - 7 * oneDay)let lastWeek = []lastWeek.push(format(oneweekdate))lastWeek.push(format(myDate))return lastWeek
}...// 使用
console.log(getWeekDate())

控制台输出结果:

最近一个月:

// 中国标准时间format yyyy-mm-dd
const format = (time) => {let ymd = ''let mouth = (time.getMonth() + 1) >= 10 ? (time.getMonth() + 1) : ('0' + (time.getMonth() + 1))let day = time.getDate() >= 10 ? time.getDate() : ('0' + time.getDate())ymd += time.getFullYear() + '-' // 获取年份。ymd += mouth + '-' // 获取月份。ymd += day // 获取日。return ymd // 返回日期。
}export const getMonthDate = () => {let nowDate = new Date()let nowDateChange = new Date()let lastMonth = []// 获取前一月时间nowDateChange.setMonth(nowDateChange.getMonth() - 1)lastMonth.push(format(nowDateChange))lastMonth.push(format(nowDate))return lastMonth
}...
// 使用console.log(getMonthDate())

输出如下:

最近三个月

// 中国标准时间format yyyy-mm-dd
const format = (time) => {let ymd = ''let mouth = (time.getMonth() + 1) >= 10 ? (time.getMonth() + 1) : ('0' + (time.getMonth() + 1))let day = time.getDate() >= 10 ? time.getDate() : ('0' + time.getDate())ymd += time.getFullYear() + '-' // 获取年份。ymd += mouth + '-' // 获取月份。ymd += day // 获取日。return ymd // 返回日期。
}export const getThreeMonthDate = () => {let nowDate = new Date()let nowDateChange = new Date()let threeMonth = []// 获取前三月时间nowDateChange.setMonth(nowDateChange.getMonth() - 3)threeMonth.push(format(nowDateChange))threeMonth.push(format(nowDate))return threeMonth
}...
// 使用
console.log(getThreeMonthDate())

输出如下:

一年

const format = (time) => {let ymd = ''let mouth = (time.getMonth() + 1) >= 10 ? (time.getMonth() + 1) : ('0' + (time.getMonth() + 1))let day = time.getDate() >= 10 ? time.getDate() : ('0' + time.getDate())ymd += time.getFullYear() + '-' // 获取年份。ymd += mouth + '-' // 获取月份。ymd += day // 获取日。return ymd // 返回日期。
}export const getYearDate = () => {let nowDate = new Date()let nowDateChange = new Date()let lastYear = []// 获取前一年时间nowDateChange.setFullYear(nowDateChange.getFullYear()- 1)lastYear.push(format(nowDateChange))lastYear.push(format(nowDate))return lastYear
}...// 使用
console.log(getYearDate())

输出如下:

三 中国标准时间/ yyyy-MM-dd/ yyyy-MM-dd HH:mm:ss转时间戳

  • 转时间戳的方法
function dateFormatStamp (date) {let stamp = new Date(date).getTime();return stamp;
}
  • 下面测试一下中国标准时间/ yyyy-MM-dd/ yyyy-MM-dd HH:mm:ss是否可行
function dateFormatStamp (date) {let stamp = new Date(date).getTime();return stamp;
}
dateFormatStamp('Mon Jun 24 2019 16:02:39 GMT+0800') //1561363359000
dateFormatStamp('2019-06-24') //1561334400000
dateFormatStamp('2019-06-24 16:02:39') //1561363359000

四 yyyy-MM-dd/yyyy-MM-dd HH:mm:ss转中国标准时间

function timeFormatDate (time) {return new Date(time)}

测试如下

function timeFormatDate (time) {return new Date(time)}
timeFormatDate('2019-06-24') //Mon Jun 24 2019 08:00:00 GMT+0800 (中国标准时间)
timeFormatDate('2019-06-24 16:00:00') //Mon Jun 24 2019 16:00:00 GMT+0800 (中国标准时间)// 其实这样也是可行的
timeFormatDate('2019/06/24') //Mon Jun 24 2019 08:00:00 GMT+0800 (中国标准时间)
timeFormatDate('2019/06/24 16:00:00') //Mon Jun 24 2019 16:00:00 GMT+0800 (中国标准时间)

目前看来,好像没问题

关于JavaScript Date 对象的方法详情请转@W3 school

更多文章请看我的博客@王一诺 感谢阅读!

js通过开始时间和结束时间计算出中间的所有日期相关推荐

  1. js通过开始时间和结束时间计算出中间的所有日期,并且转换为层级结构数组对象,用于甘特图头部日期数据

    写在前面: 先看下最终数据结构展示 time('2020-10-01', '2021-01-06') 需要根据一个开始日期和一个结束日期最后返回以下数组对象 [最外层数组里的每个对象代表了某一年的所有 ...

  2. js 通过传入 开始时间和结束时间 算出相差多少秒 分钟 相差时间

    /**  * 通过传入开始时间和结束时间  算出当前时分秒  *   * @author qhj  * @param  startTIme 开始时间  * @param  endTIme   结束时间 ...

  3. JAVA 给定开始时间和结束时间计算天数

    1.给定开始时间和结束时间计算天数 package com.ceshi;import java.text.ParseException; import java.text.SimpleDateForm ...

  4. js 限制开始时间到结束时间 最长跨度三个月

    要限制开始时间到结束时间的最长跨度为三个月,您可以使用JavaScript来实现.以下是一种可能的解决方案: const startDate = new Date('2023-04-01'); // ...

  5. java根据开始时间和结束时间计算中间间隔日期

    import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; impo ...

  6. js 获取开始时间和结束时间相隔小时及分钟(时间戳操作)

    思路:将两个时间装换成时间戳,然后结束时间减去开始时间,然后计算得到间隔的 小时 及分钟数,上代码 var st = this.form5.abnormalStartDate // 开始时间var s ...

  7. php计算一年多少周,同时计算出这一周的开始时间和结束时间(可选返回时间戳或日期)

    function getWeekStartAndEnd ($year,$week=1) { header("Content-type:text/html;charset=utf-8" ...

  8. java 根据时间范围自动算间隔_Java根据开始时间结束时间计算时间间隔 x年x月x日...

    最近项目中有这样的需求:根据租赁开始时间租赁结束时间计算租期x年x月x日, 相同的需求还有根据出生日期计算年龄等等...... 例如:开始日期 2020年7月24日  结束日期 2021年9月3日   ...

  9. vue【element ui】el-date-picker 日期选择器控件 限制可选的开始时间和结束时间

    项目场景: 总结一下日期控件实现开始日期.结束日期的选择范围限制,以便更符合实际情况. 需求: 1.开始时间和结束时间都不能选当前日期之后的时间.(当前时间:2022年5月16日) 2.先选开始时间的 ...

  10. 自定义周次开始时间,结束时间,计算一年中的周次(非自然周次)

    自定义周次开始时间,结束时间,计算一年中的周次(非自然周次),从数据库中读取时间,然后以此时间为开始时间,并且为第一周周一的开始时刻,每过7天 累计为一周,同时算出周次的开始日期和结束日期. pack ...

最新文章

  1. 线下教育地位遭冲击?“AI+教育”公司同台讲了这些事实
  2. deepfakes怎么用_如何使用 Deepfakes 换脸
  3. C# 使用反射设置某个对象的属性或读取某个对象的属性
  4. Flex Builder 2 注册码
  5. DCN RepPoints解读
  6. python 导入数据对不齐_NumPy:使用loadtxt或genfromtxt读取参差不齐的结构
  7. 初次使用cocoapods注意事项
  8. java基础之匿名内部类
  9. c++代码整洁之道pdf_别再问如何用python提取PDF内容了
  10. 音频电平vu显示表软件下载_音频控制软件-SoundSource 4 Mac
  11. Codevs 1043 方格取数
  12. 什么是分布式_什么是分布式系统,如何学习分布式系统?
  13. MyBatis拦截器原理探究MyBatis拦截器原理探究 1
  14. LeetCode 14. 最长公共前缀 (单指针水平扫描)
  15. mysql之explain详解(分析索引的最佳使用)
  16. linux下libnet编程 亲自测试可用
  17. DataNucleus之JDO操作演示样例
  18. Github爆款!Aura v2.0.0正式版来了…
  19. MATLAB 线性动态范围调整
  20. 桥接简单介绍(分类及其用法)

热门文章

  1. DA14580蓝牙硬件系统总览(二)
  2. 算法的时间复杂度和空间复杂度-总结
  3. 异数OS 开放式闭源继承人协议
  4. html表格怎么设置间距,HTML表格间距怎么设置
  5. TCP的短链接和长连接
  6. 502 Bad Gateway错误
  7. 如何快速裁剪pdf中的页面
  8. Linux 软件写文件权限,Linux 文件和目录的读写执行权限详解
  9. 内网渗透工具-反向代理nps使用分析
  10. java应聘面试自我介绍范文