Javascript Dates のまとめ - Javascript Fundamentals - JSchallenger

スポンサーリンク

はじめに

JSchallenger の Javascript Fundamentals – Javascript Dates のまとめです。
まとめページのトップは、JSchallenger まとめ です。

問題一覧と回答

Check if two dates are equal

// Sounds easy, but you need to know the trick
// Write a function that takes two date instances as arguments
// It should return true if the dates are equal
// It should return false otherwise

function myFunction(a, b) {
     return a.getTime() === b.getTime()
}

Return the number of days between two dates

// Write a function that takes two date instances as argument
// It should return the number of days that lies between those dates

function myFunction(a, b) {
   return Math.abs(a - b) / 1000 / 60 / 60 / 24;
}
function myFunction(a, b) {
   const dif = Math.abs(a - b);
   return dif / 1000 / 60 / 60 / 24
}

Check if two dates fall on the exact same day

// Write a function that takes two date instances as argument
// It should return true if they fall on the exact same day
// It should return false otherwise

1問目と同じ回答になっちゃったけど意味が違うのかな? とりあえずテストは通るが。

function myFunction(a, b) {
   return a.getTime() === b.getTime();
}
function myFunction(a, b) {
  return a.getFullYear() === b.getFullYear() &&
         a.getMonth() === b.getMonth() &&
         a.getDate()=== b.getDate()
}

Check if two dates are within 1 hour from each other

// Write a function that takes two date instances as argument
// It should return true if the difference between the dates is less than or equal to 1 hour
// It should return false otherwise

1 hour と言ってるのに右辺が 60 なのは違和感あるな。

function myFunction(a, b) {
   return Math.abs(a - b) / 1000 / 60 / 60 <= 1;
}
function myFunction(a, b) {
   return Math.abs(a - b) / 1000 / 60 <= 60
}

Check if one date is earlier than another

// Write a function that takes two date instances (a and b) as arguments
// It should return true if a is earlier than b
// It should return false otherwise

function myFunction(a, b) {
   return a < b
}

Add n days to an existing date

// Write a function that takes as argument a date instance (a) and a number (b)
// It should add b days to a and return the number of milliseconds since January 1, 1970, 00:00:00 UTC

function myFunction(a, b) {
    return a.setDate(a.getDate() + b);
}
function myFunction(a, b) {
  const currentDays = a.getDate();
  return a.setDate(currentDays + b)
}

Calculate difference between two dates in hours, minutes, and seconds

// This is a more difficult challenge
// Write a function that takes two date instances as arguments
// It should return an object with the properties ‘hrs’, ‘min’, and ‘sec’
// The corresponding values should display the absolute difference between the two dates in hours, minutes, and seconds

function myFunction(a, b) {
   const diff = Math.abs(a - b) / 1000;
   return { 
			hrs: Math.floor(diff / 60 / 60),
			min: Math.floor((diff % (60 * 60)) / 60),
			sec: diff % 60,
   };
}
function myFunction(a, b) {
  const dif = Math.abs(a - b);
  const hrs = Math.floor(dif / 1000 / 60 / 60);
  const min = Math.floor(dif / 1000 / 60) % (hrs * 60 || 60);
  const sec = Math.floor(dif / 1000) % (min * 60 + hrs * 60 * 60 || 60);
  return { hrs, min, sec }
}

Return the next nearest quarter hour of a date

// Write a function that takes a Date instance as argument
// It should return the next nearest quarter hour in minutes
// For example, if the given date has the time 10:01 the function should return 15

function myFunction(date) {
   const min = date.getMinutes();
   return min % 15 === 0 ? min : (Math.floor(min / 15) + 1) * 15 % 60;
}
function myFunction(date) {
const quarter = 15 * 60 * 1000;
const closestQuarter = new Date(Math.round(date / quarter) * quarter);
const nextQuarter = closestQuarter.getTime() < date.getTime() ? new Date(closestQuarter.getTime() + quarter) : closestQuarter;
return nextQuarter.getMinutes();
}

コメント

タイトルとURLをコピーしました