C 实现的根据日期得到今天是星期几
C#实现的根据日期得到今天是星期几
C# implementation is based on date. What day is today?
2009-05-20 12:57
Turn from:
Algorithm as follows:
Kim Larsen's formula: W= (d+2*m+3* (m+1) /5+y+y/4-y/100+y/400) mod 7
In the formula, D represents the number of days in the date, m for months, and y for years. Note: there is a difference from the other formulas in the formula:
Think of the month and February as the thirteen and fourteen months of the previous year; if it is 2004-1-10, then convert to 2003-13-10 to replace the formula.
However, when the test was found to be a little different, that is, there is a problem with the formula, we have to make minor changes:
W= (d+2*m+3* (m+1) /5+y+y/4-y/100+y/400) mod 7 +1
The code reads as follows:
According to the date of the week, the number of weeks
(Label2.Text=CaculateWeekDay (2004,12,9)); #region calculates the day of the week according to the date (Label2.Text=CaculateWeekDay (2004,12,9));
/**////
According to the date of calculation / / / week (Label2.Text=CaculateWeekDay (2004,12,9);)
/ / /
/ / /
/ / /
/ / /
/ / /
Public, static, string, CaculateWeekDay (int, y, int, m, int, d)
{
If (m==1) m=13;
If (m==2) m=14;
Int week= (d+2*m+3* (m+1) /5+y+y/4-y/100+y/400)%7+1;
String weekstr="";
Switch (week)
{
Case 1: weekstr= "Monday"; break;
Case 2: weekstr= "Tuesday"; break;
Case 3: weekstr= "Wednesday"; break;
Case 4: weekstr= "Thursday"; break;
Case 5: weekstr= "Friday"; break;
Case 6: weekstr= "Saturday"; break;
Case 7: weekstr= "Sunday"; break;
}
Return weekstr;
}
#endregion
In the process of development of the site, such as the development of news websites, forums, chat rooms, all need to get the current system date, this example describes how to obtain the system date and date system with different formats
is displayed. The example run results are shown in figure 7.1.
The code that gets the current system date and transforms it into a short date format is as follows:
Label1.Text = System.DateTime.Now.ToShortDateString ();
The code that gets the current system date and transforms it into a long date format is as follows:
Label2.Text = System.
DateTime.Now.ToLongDateString ();
Note: the Now property in the DateTime object can be replaced by the Today attribute in the DateTime object, and the DataTime.Now attribute and the DataTime.Today attribute are used to obtain the current system time.
In the development of game sites, often need to record the player's pass time, set up charts, and enhance the fun of the game. This example will show you how to get the current system time and format it. The example run results are shown in figure 7.2.
The code that gets the current system time and transforms it into a short format is as follows:
Label1.Text = System.DateTime.Now.ToShortTimeString ();
The code that gets the current system date and transforms it
into a long date format is as follows:
Label2.Text = System.DateTime.Now.ToLongTimeString ();
Some web pages display the current system, date, and time to beautify the site. This example describes how to obtain the current date and time of the system. The example run results are shown in figure 7.3.
The code to obtain the current date and time of the system is as follows:
Label1.Text = DateTime.Now.ToString ();
In this example, the main implementation is to use the DayofWeek property of the DateTime structure to get the current date of the week, and return the English word for the specified date, which is the date of the week, into the corresponding Chinese characters. The code reads as follows:
Public static string dt;
Public static string week;
Protected, void, Button1_Click (object, sender, EventArgs, e)
{
Get the current date / week
DT = DateTime.Today.DayOfWeek.ToString ();
According to the English / return Chinese characters words
Switch (DT)
{
Case "Monday"":
Week = Monday";
Break;
Case "Tuesday"":
Week = Tuesday";
Break;
Case "Wednesday"":
Week = Wednesday";
Break;
Case "Thursday"":
Week = Thursday";
Break;
Case "Friday"":
Week = Friday";
Break;
Case "Saturday"":
Week = Saturday";
Break;
Case "Sunday"":
Week = Sunday";
Break;
}
/ / use TextBox text box displays the current day of the week
TextBox1.Text = week;
}
}
This example is the main access to the specified number of days in the year, the first to use the Year property of DateTime structure to achieve the year, according to the IsLeapYear
method to determine the structure of DateTime has a year is a
leap year, if it is a leap year, "366 days", it shows that the
"365 days". The code reads as follows:
Protected, void, Button1_Click (object, sender, EventArgs, e)
{
And determine whether it is a leap year
If (DateTime.IsLeapYear (Convert.ToInt32
(DateTime.Now.Year.ToString ()))
{
/ / if it is a leap year
TextBox1.Text = 366";
}
Else
{
/ / if not a leap year
TextBox1.Text = 365";
}
}
Examples of the main access days of the current month, using the Year and Month properties of DateTime structure made the current date of the year and month, and then stay in the switch statement according to the different month of the year and returns the specified month. The code reads as follows:
Public static int year;
Public static int month;
Public static int days;
Protected, void, Button1_Click (object, sender, EventArgs, e)
{
Year = DateTime.Now.Year;
Month = DateTime.Now.Month;
Get the current month / days
Switch (month)
{
Case 1:
Days = 31;
Break;
Case 2:
If (DateTime.IsLeapYear (year))
{
/ / a leap year in February for 29 days
Days = 29;
}
Else
{
/ / not a leap year, February for 28 days
Days = 28;
}
Break;
Case 3:
Days = 31;
Break;
Case 4:
Days = 30;
Break;
Case 5:
Days = 31;
Break;
Case 6:
Days = 30;
Break;
Case 7:
Days = 31;
Break;
Case 8:
Days = 31;
天=(双)(6-firstweekfirstday);
日期时间secondweekfisrtday = mdtime。AddDays(日);第二周一
fisrtday = mdatetime。AddDays(周×7);//第N周第一天
最后一天= mdatetime。AddDays(周* 7 + 6);//第N周最后一天
一周从周一算起
日期时间mdatetime =新的日期(年,1,1);/ /年为要求的那一年
国际firstweekfirstday =转换。toint32(mdtime。周);/ /一年中第一天是周几
天=(双)(7-firstweekfirstday);
日期时间secondweekfisrtday = mdtime。AddDays(日);第二周一
fisrtday = mdatetime。AddDays(周×7);//第N周第一天
最后一天= mdatetime。AddDays(周* 7 + 6);//第N周最后一天
JavaScript:
<~DOCTYPE html PUBLIC“- / / / / W3C HTML 4 DTD的过渡/连接”>
<
<新文件>标题>
<脚本语言=“JavaScript”类型=“文本/ JavaScript”>
功能showdatecount(年,weeknum){
var date =新的日期(年,0,(7×weeknum));
VaR FD =新的日期(日期。getyear(),(日期。getmonth()),(日期。getdate()日期。getday() + 1))
VaR星期一= FD。getyear() +“-”+(FD。getmonth() + 1)+“”+ getdate() /一周的第一天FD。
VaR LD =新的日期(日期。getyear(),(日期。getmonth()),(日期。getdate()日期。getday() + 7))
VaR星期日= LD。getyear() +“-”+(LD。getmonth() + 1)+“”+ getdate() /一周的最后一天LD。
警报(年+“年第”+ weeknum +“周是“星期一+ +”至”+星期日)
}
<脚本>
“元”=“作者”内容“=”>
“元”=“关键字”内容“=”>
<元名称“=”
<头/ >
<鱼>
<输入类型“文本”价值=“2007”=“20”name=“年”>年<输入类型“文本”name=“weeknum”值=“30”=“20”>
<输入=“按钮”name=“走出去”value=“点击”onclick=“showdatecount(年。值,weeknum。值)”>
<身体>
< >
C #
/ / monthcalendar1是日历控件
system.globalization.chineselunisolarcalendar C =新chineselunisolarcalendar();
private void monthcalendar1_datechanged(object sender,DateRangeEventArgs e)
{
myselectdate = E结束日期;
txt_nonglidate.text =字符串格式
(cultureinfo.installeduiculture,“{ 0 },{ 1 },{ 2 }”,
C.
getyear (myselectdate), c.getmonth (myselectdate),
c.getdayofmonth (myselectdate));
}