This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.

This article has put together a list of six operational dates that are commonly used in daily development to help you improve your development efficiency.

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

grammar

const result = dayOfYear(date)
Copy the code

parameter

  • date (String) : specifies the datenew Date()And supportyyyy-mm-ddFormat, not to be obtained by default.

The return value

Number: specifies the day of the year in which the date is specified.

The source code

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

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
Copy the code

2. Obtain the difference between two dates

grammar

const result = getDayDiff(date1, date2, unit)
Copy the code

parameter

  • date1 (String) : specifies the date 1new Date()And supportyyyy-mm-ddFormat.
  • date2 (String) : specifies the date 2new Date()And supportyyyy-mm-ddFormat.
  • unit (String) : Sets the unit of difference. The following values are supported.
day hour minute second ms
day hours minutes seconds ms

The return value

Number: The difference between two dates.

The source code

const getDayDiff = (date1, date2, unit) = > {
  const myDate1 = typeof date1 === 'string' && date1.includes(The '-')? date1.replace(/-/g.'/') : date1;
  const myDate2 = typeof date2 === 'string' && date2.includes(The '-')? 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]));
};
Copy the code

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
Copy the code

3. Check whether the specified time is reached

It is used to perform scheduled tasks, such as changing the view when the specified time is reached. grammar

const result = isScheduled(date)
Copy the code

parameter

  • date (String) : Specifies the date in the format of “YYYY-MM-DD HH: MM :ss”.

The return value

Boolean: true reached the specified time, false not reached the specified time.

The source code

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

example

// Test date is 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
Copy the code

4. Check whether the specified date is today

grammar

const result = isToday(date)
Copy the code

parameter

  • date (String) : specifies the datenew Date()And supportyyyy-mm-ddFormat, not to be obtained by default.

The return value

Boolean: true is today, false is not today.

The source code

const isToday = (date) = > {
  // The current date
  const curDate = new Date(a);// Specify a date
  const tarData = date ? new Date(typeof date === 'string' && date.includes(The '-')? date.replace(/-/g.'/') : date) : new Date(a);return ['getFullYear'.'getMonth'.'getDate'].every((i) = > curDate[i]() === tarData[i]());
};
Copy the code

example

// Test date is 2021-09-26
const result1 = isToday(new Date())
const result2 = isToday("1998-03-09")
console.log(result1) //=> true
console.log(result2) //=> false
Copy the code

5. Check whether the specified date is in n days

grammar

const result = isTomorrow(date, n)
Copy the code

parameter

  • date (String) : specifies the datenew Date()And supportyyyy-mm-ddFormat, not to be obtained by default.
  • n (Number) :nAfter days, the default value is no1That is, tomorrow.

The return value

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

The source code

const isTomorrow = (date,  n = 1) = > {
  const curDate = new Date(a);// The current date
  curDate.setDate(curDate.getDate() + n); // The current date plus one day
  // Specify a date
  const tarData = date ? new Date(typeof date === 'string' && date.includes(The '-')? date.replace(/-/g.'/') : date) : new Date(a);return ['getFullYear'.'getMonth'.'getDate'].every((i) = > curDate[i]() === tarData[i]());
};
Copy the code

example

// Test date is 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
Copy the code

6. Check whether the specified date is n days ago

grammar

const result = isYesterday(date,  n)
Copy the code

parameter

  • date (String) : specifies the datenew Date()And supportyyyy-mm-ddFormat, not to be obtained by default.
  • n (Number) :nDays ago: No Default value1Yesterday, that is.

The return value

Boolean: true indicates n days ago. False indicates n days ago.

The source code

const isYesterday = (date, n = 1) = > {
  const curDate = new Date(a);// The current date
  curDate.setDate(curDate.getDate() - n); // Current date minus n days
  // Specify a date
  const tarData = date ? new Date(typeof date === 'string' && date.includes(The '-')? date.replace(/-/g.'/') : date) : new Date(a);return ['getFullYear'.'getMonth'.'getDate'].every((i) = > curDate[i]() === tarData[i]());
};
Copy the code

example

// Test date is 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
Copy the code

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

👉👉 Online documentation

👉👉 source code address

Other common JavaScript methods are included in the document, which we hope you can support