Home > Database > Mysql Tutorial > body text

关于SqlServer星期和C#星期被坑

WBOY
Release: 2016-06-07 15:20:56
Original
1411 people have browsed it

中国人习惯星期习惯是每周的周一是一个星期的第一天 但老外则是星期天是每周的第一天 但在sqlServer和C#中又有不同 在这里我也是被坑的很惨(先做了sqlServer的处理自以为C#和SqlServer是一样的处理就没多想 然后就。。。。。 死的很惨) 注:2015-01-5 是星期

中国人习惯星期习惯是每周的周一是一个星期的第一天 但老外则是星期天是每周的第一天

但在sqlServer和C#中又有不同 在这里我也是被坑的很惨(先做了sqlServer的处理自以为C#和SqlServer是一样的处理就没多想 然后就。。。。。 死的很惨)

注:2015-01-5 是星期一

sqlServer用函数 SELECT DATEPART(weekday, '2015-01-5')  查询返回的是 2  

C# DateTime类下面有个属性DayOfWeek  ; Convert.ToDateTime('2015-01-5').DayOfWeek;  获取返回的是 1 

SqlServer的星期是从1-7      1:星期天  以此类推 7:星期六

C#的星期是从0-6                  0:星期天  以此类推 6:星期六

如果要吧sqlServer和C#的时间星期转换成我们中国人的习惯星期 这这需要做下转换工作

sqlServer:

CREATE FUNCTION [dbo].[Get_TimeWeek] (@time datetime) returns int
 as   
    BEGIN
		declare @sdate INT
        declare @week INT
		SET @sdate= DATEPART(weekday, @time)
		SELECT @week=CASE
			when @sdate =1 then 7 
			when @sdate =2 then 1 
			when @sdate =3 then 2 
			when @sdate =4 then 3 
			when @sdate =5 then 4 
			when @sdate =6 then 5 
			when @sdate =7 then 6  
			else 0
			END  
		return @week
END 
Copy after login

C#

        public static int CaculateWeekDay(DateTime time)
        {
           
            int w = (int)time.DayOfWeek;
            int week = 0;
            switch (w)
            {
                case 0:
                    week = 7;
                    break;
                case 1:
                    week = 1;
                    break;
                case 2:
                    week = 2;
                    break;
                case 3:
                    week = 3;
                    break;
                case 4:
                    week = 4;
                    break;
                case 5:
                    week = 5;
                    break;
                case 6:
                    week = 6;
                    break;

            }
            return week;
        }
Copy after login


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template