ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

【JavaScript】封装了六个操作日期的方法

2021-11-01 19:31:56  阅读:151  来源: 互联网

标签:const JavaScript console 六个 Date 2021 new date 封装


本文整理了日常开发中常用的 6 个操作日期的方法,以帮助大家提升开发效率。

1.获取指定日期是所在年份的第几天

语法

const result = dayOfYear(date)

参数

  • date (String) : 指定日期,可传参数同 new Date(),并且支持 yyyy-mm-dd格式,不传默认获取当天。

返回值

Number : 指定日期所在年份的第几天。

源码

const dayOfYear = (date) => {
  const myData = date ? new Date(typeof date === 'string' && date.includes('-') ? date.replace(/-/g, '/') : date) : new Date();
  return Math.floor((myData - new Date(myData.getFullYear(), 0, 0)) / 1000 / 60 / 60 / 24);
};

例子

const result1 = dayOfYear()
const result2 = dayOfYear("2021,9,15")
const result3 = dayOfYear("2021-9-16")
console.log(result1) //=> 257
console.log(result2) //=> 258
console.log(result3) //=> 259

2.获取两个日期之间的差值

语法

const result = getDayDiff(date1, date2, unit)

参数

  • date1 (String) : 指定日期1,可传参数同 new Date(),并且支持 yyyy-mm-dd格式。
  • date2 (String) : 指定日期2,可传参数同 new Date(),并且支持 yyyy-mm-dd格式。
  • unit (String) : 设置差值的单位,支持以下值。
dayhourminutesecondms
小时分钟毫秒

返回值

Number : 两个日期之间的差值。

源码

const getDayDiff = (date1, date2, unit) => {
  const myDate1 = typeof date1 === 'string' && date1.includes('-') ? date1.replace(/-/g, '/') : date1;
  const myDate2 = typeof date2 === 'string' && date2.includes('-') ? date2.replace(/-/g, '/') : date2;
  const map = {
    day: 1000 * 60 * 60 * 24,
    hour: 1000 * 60 * 60,
    minute: 1000 * 60,
    second: 1000,
    ms: 1,
  };
  return Math.abs((new Date(myDate2) - new Date(myDate1)) / (map[unit]));
};

例子

// 以天为单位
const result1 = getDayDiff("2021,9,15",'2021,9,16','day')
// 以小时为单位
const result2 = getDayDiff("2021,9,15",'2021,9,16','hour')
// 以分钟为单位
const result3 = getDayDiff("2021,9,15",'2021,9,16','minute')
// 以秒为单位
const result4 = getDayDiff("2021,9,15",'2021,9,16','second')
// 以毫秒为单位
const result5 = getDayDiff("2021,9,15",'2021,9,16','ms')
console.log(result1) //=> 1
console.log(result2) //=> 24
console.log(result3) //=> 1440
console.log(result4) //=> 86400
console.log(result5) //=> 86400000

3.判断是否达到指定时间

通常用来做定时任务,达到指定时间后更改视图等操作。
语法

const result = isScheduled(date)

参数

  • date (String) : 指定日期,指定日期,格式为"YYYY-MM-DD HH:mm:ss"。

返回值

Booleantrue 达到指定时间, false 没有达到指定时间。

源码

const isScheduled = (date) => {
  const date1 = new Date();
  const date2 = new Date(Date.parse(date));
  return date1 > date2;
};

例子

//测试日期为2021-10-18
const result1 = isScheduled('2021-10-17 00:00:00')
const result2 = isScheduled('2021-10-19 00:00:00')
console.log(result1) //=> true
console.log(result2) //=> false

4.判断指定日期是不是今天

语法

const result = isToday(date)

参数

  • date (String) : 指定日期,可传参数同 new Date(),并且支持 yyyy-mm-dd格式 ,不传默认获取当天。

返回值

Booleantrue 是今天, false 不是今天。

源码

const isToday = (date) => {
  // 当前日期
  const curDate = new Date();
  // 指定日期
  const tarData = date ? new Date(typeof date === 'string' && date.includes('-') ? date.replace(/-/g, '/') : date) : new Date();
  return ['getFullYear', 'getMonth', 'getDate'].every((i) => curDate[i]() === tarData[i]());
};

例子

//测试日期为2021-09-26
const result1 = isToday(new Date())
const result2 = isToday("1998-03-09")
console.log(result1) //=> true
console.log(result2) //=> false

5.判断指定日期是不是n天后

语法

const result = isTomorrow(date, n)

参数

  • date (String) : 指定日期,可传参数同 new Date(),并且支持 yyyy-mm-dd格式 ,不传默认获取当天。
  • n (Number) : n 天后,不传默认为 1 ,也就是明天。

返回值

Booleantruen 天后, false 不是 n 天后。

源码

const isTomorrow = (date,  n = 1) => {
  const curDate = new Date(); // 当前日期
  curDate.setDate(curDate.getDate() + n); // 当前日期加一天
  // 指定日期
  const tarData = date ? new Date(typeof date === 'string' && date.includes('-') ? date.replace(/-/g, '/') : date) : new Date();
  return ['getFullYear', 'getMonth', 'getDate'].every((i) => curDate[i]() === tarData[i]());
};

例子

// 测试日期为2021-09-26
const result1 = isTomorrow(new Date())
const result2 = isTomorrow("2021-09-27",1)
const result3 = isTomorrow("2021-09-27",2)
const result4 = isTomorrow("2021-09-28",2)
console.log(result1) //=> false
console.log(result2) //=> true
console.log(result3) //=> false
console.log(result4) //=> true

6.判断指定日期是不是n天前

语法

const result = isYesterday(date,  n)

参数

  • date (String) : 指定日期,可传参数同 new Date(),并且支持 yyyy-mm-dd格式 ,不传默认获取当天。
  • n (Number) : n 天前,不传默认为 1 ,也就是昨天。

返回值

Booleantruen 天前, false 不是 n 天前。

源码

const isYesterday = (date, n = 1) => {
  const curDate = new Date(); // 当前日期
  curDate.setDate(curDate.getDate() - n); // 当前日期减n天
  // 指定日期
  const tarData = date ? new Date(typeof date === 'string' && date.includes('-') ? date.replace(/-/g, '/') : date) : new Date();
  return ['getFullYear', 'getMonth', 'getDate'].every((i) => curDate[i]() === tarData[i]());
};

例子

// 测试日期为2021-09-26
const result1 = isYesterday(new Date())
const result2 = isYesterday("2021-09-25",1)
const result3 = isYesterday("2021-09-25",2)
const result4 = isYesterday("2021-09-24",2)
console.log(result1) //=> false
console.log(result2) //=> true
console.log(result3) //=> false
console.log(result4) //=> true

本文的所有方法收录于我自己的开源仓库

标签:const,JavaScript,console,六个,Date,2021,new,date,封装
来源: https://blog.csdn.net/m0_48721669/article/details/121086665

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有