编写一个函数,计算过去或将来任何特定日期的星期几。典型的应用是计算某人出生或发生其他特殊事件的星期几。
以下是Sakamoto,Lachman,Keith和Craver建议的一个简单函数来计算日。以下函数在星期日返回0,在星期一返回1,依此类推。
C++
/* A program to find day of a given date */
#include
using namespace std;
int dayofweek(int d, int m, int y)
{
static int t[] = { 0, 3, 2, 5, 0, 3,
5, 1, 4, 6, 2, 4 };
y -= m < 3;
return ( y + y / 4 - y / 100 +
y / 400 + t[m - 1] + d) % 7;
}
// Driver Code
int main()
{
int day = dayofweek(30, 8, 2010);
cout << day;
return 0;
}
// This is code is contributed
// by rathbhupendra
C
/* A program to find day of a given date */
#include
int dayofweek(int d, int m, int y)
{
static int t[] = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 };
y -= m < 3;
return ( y + y/4 - y/100 + y/400 + t[m-1] + d) % 7;
}
/* Driver function to test above function */
int main()
{
int day = dayofweek(30, 8, 2010);
printf ("%d", day);
return 0;
}
Java
// A program to find day of a given date
class GFG
{
static int dayofweek(int d, int m, int y)
{
int t[] = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 };
y -= (m < 3) ? 1 : 0;
return ( y + y/4 - y/100 + y/400 + t[m-1] + d) % 7;
}
// Driver Program to test above function
public static void main(String arg[])
{
int day = dayofweek(30, 8, 2010);
System.out.print(day);
}
}
// This code is contributed
// by Anant Agarwal.
Python3
# Python3 program to find day
# of a given date
def dayofweek(d, m, y):
t = [ 0, 3, 2, 5, 0, 3,
5, 1, 4, 6, 2, 4 ]
y -= m < 3
return (( y + int(y / 4) - int(y / 100)
+ int(y / 400) + t[m - 1] + d) % 7)
# Driver Code
day = dayofweek(30, 8, 2010)
print(day)
# This code is contributed by Shreyanshi Arun.
C#
// C# program to find day of a given date
using System;
class GFG {
static int dayofweek(int d, int m, int y)
{
int []t = { 0, 3, 2, 5, 0, 3, 5,
1, 4, 6, 2, 4 };
y -= (m < 3) ? 1 : 0;
return ( y + y/4 - y/100 + y/400
+ t[m-1] + d) % 7;
}
// Driver Program to test above function
public static void Main()
{
int day = dayofweek(30, 8, 2010);
Console.Write(day);
}
}
// This code is contributed by Sam007.
PHP
Javascript