Advertisement
Spocoman

03. Next Day

Jan 15th, 2022
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Решение без Date() - с масив, тернарен оператор и няколко метода:
  2.  
  3. function solve(year, month, date) {
  4.     let day30 = [4, 6, 9, 11];
  5.     let monthDays = 0;
  6.     if (month === 2) {
  7.         if (year % 4 === 0) {
  8.             year % 100 !== 0 || year % 400 === 0 ? monthDays = 29 : monthDays = 28;
  9.         }
  10.     } else {
  11.         day30.includes(month) ? monthDays = 30 : monthDays = 31;
  12.     }
  13.     ++date > monthDays ? (date = 1) && month++ : date;
  14.     month === 13 ? (month = 1) && year++ : year;
  15.     console.log(year.toString().length !== 4 ? `${1900 + year}-${month}-${date}` : `${year}-${month}-${date}`);
  16.  
  17. }
  18.  
  19.  
  20. Решение с Date(), малко Regex и няколко метода:
  21.  
  22.  
  23. function nextDay(year, month, day){
  24.  
  25.     let date = new Date(year, month - 1, day + 1);
  26.     date.setDate(date.getDate() + 1);
  27.     let output = date.toISOString().substring(0, 10).split('-');
  28.     console.log(`${output[0]}-${output[1].replace(/^0+/, '')}-${output[2].replace(/^0+/, '')}`);
  29.  
  30. }
  31.  
  32.  
  33.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement