leap-year

Leap year check using bitwise operators (amazing speed)

雨燕双飞 提交于 2019-11-28 05:29:36
Someone on JSPerf dropped an amazingly fast implementation for checking leap years of the ISO calendar (link: Odd bit manipulations ): function isLeapYear(year) { return !(year & 3 || year & 15 && !(year % 25)); } Using Node.js, I quickly checked it against two other one-liner implementations I know. function isLeapClassic(y) { return (y % 4 == 0) && !(y % 100 == 0) || (y % 400 == 0); } function isLeapXOR(y) { return (y % 4 == 0) ^ (y % 100 == 0) ^ (y % 400 == 0); } function isLeapBitwise(y) { return !(y & 3 || y & 15 && !(y % 25)); } //quick'n'dirty test on a small range! //works with

Javascript: calculate number of days in month for a given year

我是研究僧i 提交于 2019-11-27 20:16:17
I have a HTML page with 3 dropdowns for the month, day and year and I was wondering if there was a way to populate the month drop down properly depending on the month and year. I haven't done this before on the client side, but it looks like a lot of controls like the jQuery DatePicker are doing that behind the scenes. You can play with date objects: var monthStart = new Date(year, month, 1); var monthEnd = new Date(year, month + 1, 1); var monthLength = (monthEnd - monthStart) / (1000 * 60 * 60 * 24) Arithmetic with Date objects gives a number of milliseconds. This will even work for December

Leap year check using bitwise operators (amazing speed)

故事扮演 提交于 2019-11-27 01:02:38
问题 Someone on JSPerf dropped an amazingly fast implementation for checking leap years of the ISO calendar (link: Odd bit manipulations): function isLeapYear(year) { return !(year & 3 || year & 15 && !(year % 25)); } Using Node.js, I quickly checked it against two other one-liner implementations I know. function isLeapClassic(y) { return (y % 4 == 0) && !(y % 100 == 0) || (y % 400 == 0); } function isLeapXOR(y) { return (y % 4 == 0) ^ (y % 100 == 0) ^ (y % 400 == 0); } function isLeapBitwise(y) {

Regular expression for asp:RegularExpressionValidator with format MMddyy (leap year issue)

て烟熏妆下的殇ゞ 提交于 2019-11-26 19:09:41
We need help for regular expression that work with asp.net asp:RegularExpressionValidator to validate date in MMddyy format. Problem we are facing is leap year. Issue is that is it possible to verify through regular expression that it only accepts valid leap year dates like 02/29/2008 is a valid date but 02/29/2010 is not a valid date. Any regular expression that works with "asp:RegularExpressionValidator"? OK, you asked for a regex. Here it is. I think it's immediately obvious why it's not a good idea to validate a date with a regular expression: First, the verbose, commented version to at

javascript to find leap year

假如想象 提交于 2019-11-26 18:49:52
How can I get the code below to work when I have a month of february? Currently it is getting to the day and then stopping before getting to the if to determine whether it is a leap year. if (month == 2) { if (day == 29) { if (year % 4 != 0 || year % 100 == 0 && year % 400 != 0) { field.focus(); field.value = month +'/' + ''; } } else if (day > 28) { field.focus(); field.value = month +'/' + ''; } } It's safer to use Date objects for datetime stuff, e.g. isLeap = new Date(year, 1, 29).getMonth() == 1 Since people keep asking about how exactly this works, it has to do with how JS calculates the

javascript to find leap year

不问归期 提交于 2019-11-26 06:37:11
问题 How can I get the code below to work when I have a month of february? Currently it is getting to the day and then stopping before getting to the if to determine whether it is a leap year. if (month == 2) { if (day == 29) { if (year % 4 != 0 || year % 100 == 0 && year % 400 != 0) { field.focus(); field.value = month +\'/\' + \'\'; } } else if (day > 28) { field.focus(); field.value = month +\'/\' + \'\'; } } 回答1: It's safer to use Date objects for datetime stuff, e.g. isLeap = new Date(year, 1

Leap year calculation

拜拜、爱过 提交于 2019-11-26 03:09:34
问题 In order to find leap years, why must the year be indivisible by 100 and divisible by 400? I understand why it must be divisible by 4. Please explain the algorithm. 回答1: The length of a year is (more or less) 365.242196 days. So we have to subtract, more or less, a quarter of a day to make it fit : 365.242196 - 0.25 = 364.992196 (by adding 1 day in 4 years) : but oops, now it's too small!! lets add a hundreth of a day (by not adding that day once in a hundred year :-)) 364.992196 + 0,01 = 365

How to find leap year programatically in C

谁说我不能喝 提交于 2019-11-26 00:48:03
问题 I made a program using C to find whether the entered year is a leap year or not. But unfortunately its not working well. It says a year is leap and the preceding year is not leap. #include<stdio.h> #include<conio.h> int yearr(int year); void main(void) { int year; printf(\"Enter a year:\"); scanf(\"%d\",&year); if(!yearr(year)) { printf(\"It is a leap year.\"); } else { printf(\"It is not a leap year\"); } getch(); } int yearr(int year) { if((year%4==0)&&(year/4!=0)) return 1; else return 0;

Java Code for calculating Leap Year

ε祈祈猫儿з 提交于 2019-11-26 00:43:53
问题 I am following \"The Art and Science of Java\" book and it shows how to calculate a leap year. The book uses ACM Java Task Force\'s library. Here is the code the books uses: import acm.program.*; public class LeapYear extends ConsoleProgram { public void run() { println(\"This program calculates leap year.\"); int year = readInt(\"Enter the year: \"); boolean isLeapYear = ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0)); if (isLeapYear) { println(year + \" is a leap year.\"); }

How to find leap year programatically in C

送分小仙女□ 提交于 2019-11-25 19:25:33
I made a program using C to find whether the entered year is a leap year or not. But unfortunately its not working well. It says a year is leap and the preceding year is not leap. #include<stdio.h> #include<conio.h> int yearr(int year); void main(void) { int year; printf("Enter a year:"); scanf("%d",&year); if(!yearr(year)) { printf("It is a leap year."); } else { printf("It is not a leap year"); } getch(); } int yearr(int year) { if((year%4==0)&&(year/4!=0)) return 1; else return 0; } After reading the comments i edited my coding as: #include<stdio.h> #include<conio.h> int yearr(int year);