Java教程

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

本文主要是介绍【JavaScript】封装了六个操作日期的方法,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

本文整理了日常开发中常用的 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

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

这篇关于【JavaScript】封装了六个操作日期的方法的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!