-5

I should to write a function isLeapYear.t should accept a number of millisecond or string of date as an argument. This function checks if a passed argument is a Leap Year. If it is a Leap Year, function should return a string – ‘ “year” is a leap year’ and if it isn’t, should return string - ‘ “year” is not a leap year’. I should work with Date object. I know that this work not as i want

if (year % 400 == 0)
  leap = true;
else if (year % 100 == 0)
  leap = false;
else if (year % 4 == 0)
  leap = true;
else
  leap = false;

For Example isLeapYear('2020-01-01 00:00:00'); // =>  ‘2020 is a leap year’
isLeapYear('2020-01-01 00:00:00777'); // =>  ‘Invalid Date’
isLeapYear('2021-01-15 13:00:00');  // =>  ‘2021 is not a leap year’
isLeapYear('2200-01-15 13:00:00'); // =>  ‘2200 is not a leap year’
isLeapYear(1213131313135465656654564646542132132131); // =>  ‘Invalid Date’
isLeapYear(1213131313); ); // => ‘1970 is not a leap year’
  • 5
    Welcome to Stack Overflow! *"I know that this work not as i want"* In what way? Once you've answered that, your next task is to try to fix that. Please take the [tour] (you get a badge!) and read through the [help], in particular [*How do I ask a good question?*](/help/how-to-ask) Your best bet here is to do your research, [search](/help/searching) for related topics on SO, and give it a go. ***If*** you get stuck and can't get unstuck after doing more research and searching, post a [mcve] of your attempt and say specifically where you're stuck. People will be glad to help. – T.J. Crowder Apr 10 '21 at 14:06
  • Your issue seems to be turning the string into a Date, see [*Converting a string to a date in JavaScript*](https://stackoverflow.com/questions/5619202/converting-a-string-to-a-date-in-javascript) and [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) – RobG Apr 10 '21 at 21:35

1 Answers1

-1

I assume you want JS, despite the example being in gods know what

const isLeapYearHelper = (year) => (year % 400 == 0 ? true : year % 100 == 0 ? false : year % 4 == 0 ? true : false)

const isLeapYear = (str) => {
const date = new Date(str)
const isValid = !isNaN(date.getTime())
return isValid ? isLeapYearHelper(date.getFullYear()) ? `${date.getFullYear()} is a leap year` : `${date.getFullYear()} is a not leap year` : 'Invalid Date'
}

console.log(isLeapYear('2020-01-01 00:00:00777'))
console.log(isLeapYear('2021-01-15 13:00:00'))
console.log(isLeapYear('2200-01-15 13:00:00'))
console.log(isLeapYear('2020-01-15 13:00:00'))
console.log(isLeapYear(1213131313135465656654564646542132132131))
console.log(isLeapYear(1213131313))
Vulwsztyn
  • 2,140
  • 1
  • 12
  • 20
  • [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) – RobG Apr 10 '21 at 21:34
  • Please do explain – Vulwsztyn Apr 10 '21 at 22:34
  • "*Until the 5th edition spec came out, the Date.parse method was completely implementation dependent… In the 5th edition spec the requirement was added to support a simplified (and slightly incorrect) ISO-8601 (also see What are valid Date Time Strings in JavaScript?). But other than that, there was no requirement for what Date.parse / new Date(string) should accept other than that they had to accept whatever Date#toString output…*" – RobG Apr 11 '21 at 03:46