zoukankan      html  css  js  c++  java
  • 指定周转对应日期

    SQL 根据某年某周获取对应日期

    --创建一个标量值函数

    --例:WeekToDay(2018,18) 

    CREATE function [dbo].[WeekToDay](@Year int,@Week int) 
    returns varchar(50)
    as
    begin

    declare @Year1 datetime
    declare @StratWeek datetime
    declare @EndWeek datetime

    set @Year1=convert(smalldatetime,convert(varchar(4),@Year) + '-1-1')
    set @StratWeek=dateadd(day,1-datePart(weekday,@Year1),dateAdd(week,@Week-1,@Year1))
    set @EndWeek=dateadd(day,6,@StratWeek)

    RETURN convert(varchar(10),@StratWeek,111)

    end

    C#  根据某年某周获取对应日期

    例:  GetFirstDayOfWeek(year, week, System.Globalization.CultureInfo.InvariantCulture); 

    /// <summary>
    /// 根据一年中的第几周获取该周的开始日期
    /// </summary>
    /// <param name="year"></param>
    /// <param name="week"></param>
    /// <param name="culture"></param>
    /// <returns></returns>
    public static DateTime GetFirstDayOfWeek(int year, int week, System.Globalization.CultureInfo culture)
    {
    System.Globalization.Calendar calendar = culture.Calendar;
    DateTime firstOfYear = new DateTime(year, 1, 1, calendar);
    DateTime targetDay = calendar.AddWeeks(firstOfYear, week - 1);
    DayOfWeek firstDayOfWeek = culture.DateTimeFormat.FirstDayOfWeek;

    while (targetDay.DayOfWeek != firstDayOfWeek)
    {
    targetDay = targetDay.AddDays(-1);
    }

    return targetDay;
    }

  • 相关阅读:
    曾国藩谕纪泽纪鸿
    简单实现KeyChain实例
    UUID、UDID和KeyChain
    iOS沙盒目录结构解析 (转)
    BOOL布尔类型
    表达式
    赋值运算符
    变量
    常量
    GET请求和POST请求简单说明
  • 原文地址:https://www.cnblogs.com/limitpjz/p/10792037.html
Copyright © 2011-2022 走看看