代码之家  ›  专栏  ›  技术社区  ›  AGoodDisplayName

如何从两个日期得到月数和天数

  •  3
  • AGoodDisplayName  · 技术社区  · 14 年前

    我有两张桌子:

    AgeMilestones //Represents available timespans
    Id int     
    Description varchar //(newborn, 1 month old, 2 month old, etc.)
    NbrMonths int //( 0, 1, 2, etc.)
    NbrDays int //(0, 1, 2, etc.)   
    
    
    Child
    Id int     
    DateOfBirth DateTime  
    

    目前 NBR月 进了几天,我可能偶尔休息几天。

    编辑:
    我需要弄清楚什么是agemilesstone对应于从孩子出生到今天这段时间内的月/日数(类似于下面的内容)。我被绊倒的情况下,年龄里程碑可能是3个月15天,或5个月7天。。。

    SET @Days = DateDiff(d,child.DateOfBirth, GetDate())  
    SET @Months = DateDiff(m,child.DateOfBirth, GetDate()) 
    
    SELECT * FROM AgeMileStone WHERE NbrMonths < @Months AND NbrDays < @Days 
    


    像这样的记录有问题吗
    阿梅利斯通:
    编号:4
    描述:“5个月和1/2个月”

    天数:15

    4 回复  |  直到 14 年前
        1
  •  1
  •   David    14 年前

    我相信这能解决问题,因为 DATEADD 函数将负责添加出生日期的月份和天数:

    declare @AgeMilestones table (
        NbrMonths int not null,
        NbrDays int not null,
        [Description] varchar(64) not null
    )
    
    declare @Child table (
        ChildId int not null identity,
        Name varchar(32) not null,
        DateOfBirth datetime not null
    )
    
    insert @AgeMilestones values (5, 15, '5 and 1/2 months')
    insert @AgeMilestones values (0, 0, 'newborn')
    
    insert @Child values ( 'Yearling', '2010-01-01' )
    insert @Child values ( 'Newborn', GETDATE() )
    
    declare @currentChild int = 2
    
    select
        m.*
    from @Child c
    inner join @AgeMilestones m
        on dateadd(month, m.NbrMonths, dateadd(day, m.NbrDays, c.DateOfBirth)) <= getdate()
    where c.ChildId = @currentChild
    
        2
  •  2
  •   Denis Valeev    14 年前

    datediff(month, DOB, getdate()) .

    像这样:

    declare @dob datetime = getdate() - 123; --born 123 days ago
    
    select cast(datediff(month, @dob, getdate()) as varchar) + ' month old'
    ,cast(datediff(day, @dob, getdate()) as varchar) + ' days old'
    

    更新

    declare @dob datetime;
    set @dob = getdate() - 125;
    
    select 
    datediff(month, @dob, getdate()) [Months], 
    datediff(day, dateadd(month, datediff(month, @dob, getdate()), @dob), getdate()) [Offset Days]
    
        3
  •  1
  •   Michael Eakins    14 年前

    我建议使用这样的方法:

    DATEPART(Month, NOW())
    DATEPART(DAY, NOW())
    

    尝试使用

        4
  •  1
  •   Adam Porad    14 年前

    下面是一个查询,它使用您拥有的agmeilestones表和 DATEADD 函数,该函数返回在特定日期出生的孩子的里程碑列表。

    -- setup the AgeMilestone table with some initial data
    CREATE table AgeMilestone (milestone_month int, milestone_name varchar(50))
    insert into AgeMilestone (milestone_month, milestone_name) values (1, '1 month')
    insert into AgeMilestone (milestone_month, milestone_name) values (2, '2 month')
    insert into AgeMilestone (milestone_month, milestone_name) values (3, '3 month')
    insert into AgeMilestone (milestone_month, milestone_name) values (4, '4 month')
    ...
    insert into AgeMilestone (milestone_month, milestone_name) values (12, '12 month')
    insert into AgeMilestone (milestone_month, milestone_name) values (24, '24 month')
    
    Declare @DOB DATETIME = '1/14/2009'
    SELECT 
         milestone_month, milestone_name
    FROM AgeMilestone
    where DATEADD(month, milestone_month, @DOB) <= GETDATE()