[JavaScript] encapsulates the method of six operation dates

This paper sorts out the methods of 6 operation dates commonly used in daily development to help everyone improve development efficiency.

1. Get the day of the year in which the specified date is

grammar

const result = dayOfYear(date)

parameter

  • date (String): Specifies the date. The parameters that can be transferred are the same as new Date(), and support yyyy MM DD format. The current day is obtained by default without transferring.

Return value

Number: Specifies the day of the year in which the date is located.

Source code

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);
};

example

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. Get the difference between two dates

grammar

const result = getDayDiff(date1, date2, unit)

parameter

  • date1 (String): Specifies date 1. The transferable parameters are the same as new Date(), and support yyyy MM DD format.
  • date2 (String): Specifies date 2. The transferable parameters are the same as new Date(), and support yyyy MM DD format.
  • unit (String): sets the unit of difference. The following values are supported.
dayhourminutesecondms
dayhourminutesecondmillisecond

Return value

Number: the difference between two dates.

Source code

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]));
};

example

// In days
const result1 = getDayDiff("2021,9,15",'2021,9,16','day')
// In hours
const result2 = getDayDiff("2021,9,15",'2021,9,16','hour')
// In minutes
const result3 = getDayDiff("2021,9,15",'2021,9,16','minute')
// In seconds
const result4 = getDayDiff("2021,9,15",'2021,9,16','second')
// In Milliseconds 
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. Judge whether the specified time has been reached

It is usually used to do scheduled tasks and change the view after reaching the specified time.
grammar

const result = isScheduled(date)

parameter

  • date (String): Specifies the date. The format is "YYYY-MM-DD HH:mm:ss".

Return value

Boolean: true reaches the specified time, false does not reach the specified time.

Source code

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

example

//The test date is October 18, 2021
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. Judge whether the specified date is today

grammar

const result = isToday(date)

parameter

  • date (String): Specifies the date. The parameters that can be transferred are the same as new Date(), and support yyyy MM DD format. The current day is obtained by default without transferring.

Return value

Boolean: true is today, false is not today.

Source code

const isToday = (date) => {
  // current date
  const curDate = new Date();
  // Specify 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]());
};

example

//The test date is September 26, 2021
const result1 = isToday(new Date())
const result2 = isToday("1998-03-09")
console.log(result1) //=> true
console.log(result2) //=> false

5. Judge whether the specified date is n days later

grammar

const result = isTomorrow(date, n)

parameter

  • date (String): Specifies the date. The parameters that can be transferred are the same as new Date(), and support yyyy MM DD format. The current day is obtained by default without transferring.
  • n (Number): after n days, it will not be transmitted. The default value is 1, that is, tomorrow.

Return value

Boolean: true is n days later, false is not n days later.

Source code

const isTomorrow = (date,  n = 1) => {
  const curDate = new Date(); // current date
  curDate.setDate(curDate.getDate() + n); // Current date plus one day
  // Specify 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]());
};

example

// The test date is September 26, 2021
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. Judge whether the specified date is n days ago

grammar

const result = isYesterday(date,  n)

parameter

  • date (String): Specifies the date. The parameters that can be transferred are the same as new Date(), and support yyyy MM DD format. The current day is obtained by default without transferring.
  • n (Number): n days ago, it is not transmitted. The default is 1, that is, yesterday.

Return value

Boolean: true is n days ago, false is not n days ago.

Source code

const isYesterday = (date, n = 1) => {
  const curDate = new Date(); // current date
  curDate.setDate(curDate.getDate() - n); // Current date minus n days
  // Specify 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]());
};

example

// The test date is September 26, 2021
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

All the methods in this article are included in my own open source repository

👉👉 Online documentation

👉👉 Source address

Other common JavaScript methods are also sorted out in the document. I hope you can support them~

Tags: Javascript ECMAScript

Posted on Mon, 01 Nov 2021 08:16:08 -0400 by pernest