有DateDiff显示小数 [英] Have DateDiff Show Decimals

查看:985
本文介绍了有DateDiff显示小数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用DateDiff函数,但我希望它给我3个小数位.如何更改我的查询以实现这种结果? -我需要通过查询本身而不是VBA函数来完成此操作.

I am using the DateDiff function, but I would like for it to give me 3 decimal places. How should my query be altered to achieve such result? -- I need this done via the query itself not a VBA function.

Date123: DateDiff('d', [startdate], [enddate])

推荐答案

对于您可以在查询中添加的行,我将使用类似以下的内容.

For a line that you can just put into a query, I'd use something like the following.

Format(DateDiff("s",[DateOne],[DateTwo])/60/60/24,"#.###")

更好的做法是在数据库的模块中创建一个函数,如下所示.然后通过查询调用它.该代码更具可维护性,可测试性和可理解性.

Better practice would be to create a function in a module in your db like the following. Then call it through the query. The code is more maintainable, testable and understandable.

Public Function DateDiffInFractions(DateOne As Date, DateTwo As Date) As String
    Dim SecondsDiff As Double
    SecondsDiff = DateDiff("S", DateOne, DateTwo) / 24 / 60 / 60    'Days/hours/minutes/seconds
    DateDiffInFractions = Format(SecondsDiff, "0.000")              'Format to 3 Decimal points. Return string
End Function

这篇关于有DateDiff显示小数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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