两个日期相比较,计算出相隔几年几月几日?例如:2002年12月1日和2004年1月5日比较,得到相隔一年零一个月四日,代码怎么写?

解决方案 »

  1.   

    datediff()能算想差多少天,月或者年
    如果全算的,可以要拆分来算
      

  2.   

    使用函数diff……我有点记不清了
      

  3.   

    自己测试一下就知道了:
    DateDiff Function Example
    This example uses the DateDiff function to display the number of days between a given date and today.Dim TheDate As Date   ' Declare variables.
    Dim Msg
    TheDate = InputBox("Enter a date")
    Msg = "Days from today: " & DateDiff("d", Now, TheDate)
    MsgBox Msg
    --------------------------------------------------------------------------------DateDiff Function
          Returns a Variant (Long) specifying the number of time intervals between two specified dates.SyntaxDateDiff(interval, date1, date2[, firstdayofweek[, firstweekofyear]])The DateDiff function syntax has these named arguments:Part Description 
    interval Required. String expression that is the interval of time you use to calculate the difference between date1 and date2. 
    date1, date2 Required; Variant (Date). Two dates you want to use in the calculation. 
    firstdayofweek Optional. A constant that specifies the first day of the week. If not specified, Sunday is assumed. 
    firstweekofyear Optional. A constant that specifies the first week of the year. If not specified, the first week is assumed to be the week in which January 1 occurs. 
    SettingsThe interval argument has these settings:Setting Description 
    yyyy Year 
    q Quarter 
    m Month 
    y Day of year 
    d Day 
    w Weekday 
    ww Week 
    h Hour 
    n Minute 
    s Second 
    The firstdayofweek argument has these settings:Constant Value Description 
    vbUseSystem 0 Use the NLS API setting. 
    vbSunday 1 Sunday (default) 
    vbMonday 2 Monday 
    vbTuesday 3 Tuesday 
    vbWednesday 4 Wednesday 
    vbThursday 5 Thursday 
    vbFriday 6 Friday 
    vbSaturday 7 Saturday Constant Value Description 
    vbUseSystem 0 Use the NLS API setting. 
    vbFirstJan1 1 Start with week in which January 1 occurs (default). 
    vbFirstFourDays 2 Start with the first week that has at least four days in the new year. 
    vbFirstFullWeek 3 Start with first full week of the year. 
    ResYou can use the DateDiff function to determine how many specified time intervals exist between two dates. For example, you might use DateDiff to calculate the number of days between two dates, or the number of weeks between today and the end of the year.To calculate the number of days between date1 and date2, you can use either Day of year ("y") or Day ("d"). When interval is Weekday ("w"), DateDiff returns the number of weeks between the two dates. If date1 falls on a Monday, DateDiff counts the number of Mondays until date2. It counts date2 but not date1. If interval is Week ("ww"), however, the DateDiff function returns the number of calendar weeks between the two dates. It counts the number of Sundays between date1 and date2. DateDiff counts date2 if it falls on a Sunday; but it doesn't count date1, even if it does fall on a Sunday.If date1 refers to a later point in time than date2, the DateDiff function returns a negative number.The firstdayofweek argument affects calculations that use the "w" and "ww" interval symbols.If date1 or date2 is a date literal, the specified year becomes a permanent part of that date. However, if date1 or date2 is enclosed in double quotation s (" "), and you omit the year, the current year is inserted in your code each time the date1 or date2 expression is evaluated. This makes it possible to write code that can be used in different years.When comparing December 31 to January 1 of the immediately succeeding year, DateDiff for Year ("yyyy") returns 1 even though only a day has elapsed.Note   For date1 and date2, if the Calendar property setting is Gregorian, the supplied date must be Gregorian.  If the calendar is Hijri, the supplied date must be Hijri. 
      

  4.   

    呵呵,楼上的MSDN在哪里下载的啊???可否告诉我啊???嘻嘻
      

  5.   

    datediff()函数我知道,问题是算出相隔多少天后,怎么算是几年几月几日?
      

  6.   

    你的问题有点奇怪:年、月的天数都是不确定的呀!
    如果一定要算,可以用datediff()算多次:
    1 先用间隔"M"算出相差多少个月。得数\12 得年;得数 mod 12 得月。
    2 用dateadd("M", <得数>, <较早的日期>)算出一个中间日期;
    3 用datediff("d",<中间日期>,<最后日期>)算出天数
      

  7.   

    呵呵,中文版来啦
     
    DateDiff 函数
          返回 Variant (Long) 的值,表示两个指定日期间的时间间隔数目。语法DateDiff(interval, date1, date2[, firstdayofweek[, firstweekofyear]])DateDiff 函数语法中有下列命名参数:部分 描述 
    interval 必要。字符串表达式,表示用来计算date1 和 date2 的时间差的时间间隔 
    Date1□date2 必要;Variant (Date)。计算中要用到的两个日期。 
    Firstdayofweek 可选。指定一个星期的第一天的常数。如果未予指定,则以星期日为第一天。 
    firstweekofyear 可选。指定一年的第一周的常数。如果未予指定,则以包含 1 月 1 日的星期为第一周。 
    设置interval 参数的设定值如下:设置 描述 
    yyyy 年 
    q 季 
    m 月 
    y 一年的日数 
    d 日 
    w 一周的日数 
    ww 周 
    h 时 
    n 分钟 
    s 秒 
    firstdayofweek 参数的设定值如下:常数 值 描述 
    vbUseSystem 0 使用 NLS API 设置。 
    vbSunday 1 星期日(缺省值) 
    vbMonday 2 星期一 
    vbTuesday 3 星期二 
    vbWednesday 4 星期三 
    vbThursday 5 星期四 
    vbFriday 6 星期五 
    vbSaturday 7 星期六 常数 值 描述 
    vbUseSystem 0 用 NLS API 设置。 
    vbFirstJan1 1 从包含 1 月 1 日的星期开始(缺省值)。 
    vbFirstFourDays 2 从第一个其大半个星期在新的一年的一周开始。 
    vbFirstFullWeek 3 从第一个无跨年度的星期开始。 
    说明DateDiff 函数可用来决定两个日期之间所指定的时间间隔数目。例如,可以使用 DateDiff 来计算两个日期之间相隔几日,或计算从今天起到年底还有多少个星期。为了计算 date1 与 date2 相差的日数,可以使用“一年的日数”(y) 或“日”(d)。当 interval 是“一周的日数”(w) 时,DateDiff 返回两日期间的周数。如果 date1 是星期一,DateDiff 计算到 date2 为止的星期一的个数。这个数包含 date2 但不包含 date1。不过,如果 interval 是“周”(ww),则 DateDiff 函数返回两日期间的“日历周”数。由计算 date1 与 date2 之间星期日的个数而得。如果 date2 刚好是星期日,则 date2 也会被加进 DateDiff 的计数结果中;但不论 date1 是否为星期日,都不将它算进去。如果 date1 比 date2 来得晚,则 DateDiff 函数的返回值为负数。firstdayofweek 参数会影响使用时间间隔符号 “W” 或 “WW” 计算的结果。如果 date1 或 date2 是日期文字,则指定的年份成为该日期的固定部分。但是,如果 date1 或 date2 用双引号 (" ") 括起来,且年份略而不提,则在每次计算表达式 date1 或 date2 时,当前年份都会插入到代码之中。这样就可以书写适用于不同年份的程序代码。在计算 12 月 31 日和来年的 1 月 1 日的年份差时,DateDiff 返回 1 表示相差一个年份,虽然实际上只相差一天而已。
      

  8.   

    用datediff("d",起始日期,结束日期)其中"d"的意思就是用天数来算。用月是m之类的,去装个msdn有帮助
      

  9.   

    至于你的想得到另一种描述的问题,看我的
    '把秒数转换成日时分秒,年之后的就没多考虑了
    Private Sub Command2_Click()
        Dim miao As Long
        Dim fen As Long
        Dim shi As Long
        Dim tian  As Long    miao = Int(Text1.Text) Mod 60
        
        fen = (Int(Text1.Text) \ 60) Mod 60
        
        shi = ((Int(Text1.Text) \ 60) \ 60) Mod 24
        
        tian = (((Int(Text1.Text) \ 60) \ 60) \ 24) Mod 365
        
        MsgBox tian & " 天 " & shi & " 时 " & fen & " 分 " & miao & " 秒"
    End Sub
      

  10.   

    MsgBox Format(DateSerial(0, 0, DateDiff("D", #1/1/2004#, #6/8/2007#)), "YY 年 M月 D天")
      

  11.   

    '日期比较问题?
    '你好,多谢你的回复
    '我是想一个日期跟系统日期对比,算出人的岁数是多少岁几个月几日
    '例如:2002年12月1日和2004年1月5日比较,得到一岁零一个月四日,代码怎么写?简单示例,实际上涉及到闰年和闰月,还比较麻烦
    Private Sub Command1_Click()
        Dim nTian As Long
        Dim nMonth As Long
        Dim nYear As Long
        
        
        Dim strDiff As String
        strDiff = DateDiff("d", Format("2002年11月1日", "yyyy-mm-dd"), Now)
        
        nTian = Int(strDiff) Mod 31
        
        nMonth = (Int(strDiff) \ 31) Mod 12
        
        nYear = (Int(strDiff) \ 31 \ 12) Mod 365
        
        MsgBox nYear & " 年" & nMonth & " 月" & nTian & " 天 "End Sub
      

  12.   

    ---- (1)DateDiff 函数 ---- 语法 DateDiff(interval, date1, date2[, firstdayofweek[, firstweekofyear]]) 返回 Variant (Long) 的值,表示两个指定日期间的时间间隔数目。DateDiff 函数语法中有下列命名参数: ---- interval 必要。字符串表达式,表示用来计算date1 和 date2 的时间差的时间间隔 Date1,date2 必要;Variant (Date)。计算中要用到的两个日期。 ---- Firstdayofweek 可选。指定一个星期的第一天的常数。如果未予指定,则以星期日为第一天。 ---- firstweekofyear 可选。指定一年的第一周的常数。如未指定,则以包含 1 月 1 日的星期为第一周。 ---- interval 参数的设定值如下(我们设置为s即秒): ---- yyyy 年;q 季 ;m 月 ;y 一年的日数 ;d 日 ;w 一周的日数 ;ww 周 ;h 时 ; ---- n 分钟 ;s 秒 
      

  13.   

    关注 ing顶!'northwolves(野性的呼唤) ( )  'MsgBox Format(DateSerial(0, 0, DateDiff("D", #1/1/2004#, #6/8/2007#)), "YY 年 M月 D天")
      
     
    这位兄弟所说的方法好象可用。
      

  14.   

    (1)DateDiff 函数 ---- 语法 DateDiff(interval, date1, date2[, firstdayofweek[, firstweekofyear]]) 返回 Variant (Long) 的值,表示两个指定日期间的时间间隔数目。DateDiff 函数语法中有下列命名参数: ---- interval 必要。字符串表达式,表示用来计算date1 和 date2 的时间差的时间间隔 Date1,date2 必要;Variant (Date)。计算中要用到的两个日期。 ---- Firstdayofweek 可选。指定一个星期的第一天的常数。如果未予指定,则以星期日为第一天。 ---- firstweekofyear 可选。指定一年的第一周的常数。如未指定,则以包含 1 月 1 日的星期为第一周。 ---- interval 参数的设定值如下(我们设置为s即秒): ---- yyyy 年;q 季 ;m 月 ;y 一年的日数 ;d 日 ;w 一周的日数 ;ww 周 ;h 时 ; ---- n 分钟 ;s 秒 ---- (2)GetObject 函数 
      

  15.   

    http://www.1000vb.net/html/gb/exp2/QQExp053.html#datediff
    利用其中的datediff函数