我如何计算特定月份的SUNDAY [英] How do i count SUNDAY's in a particular MONTH

查看:159
本文介绍了我如何计算特定月份的SUNDAY的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好朋友,

i已经开发了一个关于员工出勤的应用程序,我想计算一个月中的星期日 ex.november 。怎么做无论是在.net还是在SQL中。我只是想计算周日。



我想让它在这些天自动在数据库中插入HOLIDAY。为所有员工。我有考勤表,我参加了出勤日期,时间,状态(无论是出席,缺席,假日)等字段。



i试图在.net和SQL中计算,但它对我不起作用。

感谢任何形式的帮助。





谢谢

Hello friends,
i have developed one application on "Employee Attendance" for which i want to calculate sundays in a particular month ex.november. how do do it. no matter whether it's in .net or in SQL. i just want to calculate sundays.

and i want it to automatically insert "HOLIDAY" in the database on these days. for all the employees. i have attendance table in which i took fields like attendance date, time, status(whether Present, Absent, Holiday).

i have tried to calculate in .net as well as in SQL but it didn't worked for me.
Any kind of help is appreciated.


Thanks

推荐答案

你可以在MS-SQL中试试它



You Can try it in MS-SQL Like

Declare @Month Bigint
Set @Month =11
Declare @date date;
Set @date='2011-11-01'

while DATEPART(Month,@date)=@Month
Begin
	--Select DATEPART(DAY,@date);
	--Select DATEPART( WEEKDAY, @DATE )
	DECLARE @Name VARCHAR(20)

	SELECT  @Name = CASE ( DATEPART(dw, @Date) + @@DATEFIRST ) % 7
                         WHEN 1 THEN 'Sunday'
                         WHEN 2 THEN 'Monday'
                         WHEN 3 THEN 'Tuesday'
                         WHEN 4 THEN 'Wednesday'
                         WHEN 5 THEN 'Thursday'
                         WHEN 6 THEN 'Friday'
                         WHEN 0 THEN 'Saturday'
                       END 
	If @Name='Sunday'
	Begin
		
		--Insert Data Into Your Table
		Select @date
	End
	
	Set @date=DATEADD(Day,1,@date);	
End


查看:

http://www.aspdotnet-suresh.com/2010/ 09 / discover-sundays-in-given-month-using.html [ ^ ]


看起来您想要计算特定月份的星期日。您可以使用以下C#代码。



Looks like you want to count Sundays in a particular month. You can use the following C# code.

// The year (1 through 9999).
// The month (1 through 12).
static int CountSundays(int year, int month)
{
	var firstDay = new DateTime(year,month , 1);

	var day29 = firstDay.AddDays(28);
	var day30 = firstDay.AddDays(29);
	var day31 = firstDay.AddDays(30);

	if ((day29.Month == month && day29.DayOfWeek == DayOfWeek.Sunday)
	|| (day30.Month == month && day30.DayOfWeek == DayOfWeek.Sunday)
	|| (day31.Month == month && day31.DayOfWeek == DayOfWeek.Sunday))
	{
		return 5;
	}
	else
	{
		return 4;
	}
}





------------

U Zafar

SE在TradeMeters POS软件(http://www.TradeMeters.com)



------------
U Zafar
SE at TradeMeters POS Software ( http://www.TradeMeters.com )


这篇关于我如何计算特定月份的SUNDAY的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆