DateDiff函数 [英] DateDiff Function

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

问题描述

我正在尝试使用DateDiff函数来计算差异

是否已设置班次。当我用

strFirstShiftEnd作为字符串或日期时间运行下面的代码时。我收到错误

参数Date1无法转换为Date类型。任何人都清楚这个

up。


如果DateDiff(n,格式(现在,hh:mm AMPM),格式(strFirstShiftEnd) ,

" hh:mm AMPM"))< = 0然后

''退出

MsgBox(换班已有你将被记录下来。

现在出来。")

结束如果

I''m trying to use the DateDiff function to calculate the difference
whether a shift has been setup. when i run the code below with
strFirstShiftEnd as a stringor date or datetime. I get an error
Argument Date1 cannot be converted to type Date. an anyone clear this
up.

If DateDiff("n", Format(Now, "hh:mm AMPM"), Format(strFirstShiftEnd,
"hh:mm AMPM")) <= 0 Then
''logout
MsgBox("A shift change has occurred. You will be logged
out now.")
End If

推荐答案

cmdolcet69< co ************ @ hotmail.comwrote:
cmdolcet69 <co************@hotmail.comwrote:

我正在尝试使用DateDiff函数来计算差价

是否已设置班次。当我用

strFirstShiftEnd作为字符串或日期时间运行下面的代码时。我收到错误

参数Date1无法转换为Date类型。任何人都清楚这个

up。


如果DateDiff(n,格式(现在,hh:mm AMPM),格式(strFirstShiftEnd) ,

" hh:mm AMPM"))< = 0然后

''退出

MsgBox(换班已有你将被记录下来

现在。)

结束如果
I''m trying to use the DateDiff function to calculate the difference
whether a shift has been setup. when i run the code below with
strFirstShiftEnd as a stringor date or datetime. I get an error
Argument Date1 cannot be converted to type Date. an anyone clear this
up.

If DateDiff("n", Format(Now, "hh:mm AMPM"), Format(strFirstShiftEnd,
"hh:mm AMPM")) <= 0 Then
''logout
MsgBox("A shift change has occurred. You will be logged
out now.")
End If



好​​吧,显而易见的问题是Format返回一个字符串,而不是DateDiff需要的
DateTime。


之后,那就是你的问题正在尝试使用

格式,但这需要更多信息...


-

JB Moreno

Well, the obvious problem is that Format returns a string, not a
DateTime, which is what DateDiff requires.

After that, there''s the question of what you were trying to do with the
Format, but that would require more info...

--
J.B. Moreno


" cmdolcet69" < co ************ @ hotmail.comschrieb
"cmdolcet69" <co************@hotmail.comschrieb

我正在尝试使用DateDiff函数来计算差异

是否已设置班次。当我用

strFirstShiftEnd作为字符串或日期时间运行下面的代码时。我收到错误

参数Date1无法转换为Date类型。任何人都清楚

这个。


如果DateDiff(n,格式(现在,hh:mm AMPM),格式(strFirstShiftEnd) ,

" hh:mm AMPM"))< = 0然后

''退出

MsgBox(换班已有你将被记录下来

out。")

结束如果
I''m trying to use the DateDiff function to calculate the difference
whether a shift has been setup. when i run the code below with
strFirstShiftEnd as a stringor date or datetime. I get an error
Argument Date1 cannot be converted to type Date. an anyone clear
this up.

If DateDiff("n", Format(Now, "hh:mm AMPM"), Format(strFirstShiftEnd,
"hh:mm AMPM")) <= 0 Then
''logout
MsgBox("A shift change has occurred. You will be logged
out now.")
End If



永远不要使用字符串试着用。来计算。使用Date和TimeSpan数据

类型来存储这些值。仅在需要时转换为/来自String,例如for

视觉显示/输入或文件I / O.


一个例子:


Dim d1,d2作为日期

Dim ts As TimeSpan


d1 = Date.Parse(InputBox(输入日期))

d2 =现在

ts = d1.Subtract(d2)

MsgBox(差异是:& ts.TotalMinutes&分钟" ;)


所以,

- 日期(实际上是DateTime)存储任何时间点(没有

持续时间) )。

- TimeSpan存储两个时间点之间的惊喜时间跨度。你可以通过访问Timespan的

成员来获得你想要的任何单位的持续时间。

Armin

Never use Strings to try to calculate with. Use the Date and TimeSpan data
types to store these values. Convert to/from String only if needed, e.g. for
visual display/input or file I/O.

An example:

Dim d1, d2 As Date
Dim ts As TimeSpan

d1 = Date.Parse(InputBox("enter a date"))
d2 = Now
ts = d1.Subtract(d2)

MsgBox("Difference is: " & ts.TotalMinutes & " minutes")

So,
- Date (actually DateTime) stores any point in time (which does not have a
duration).
- TimeSpan stores the, surprise, time span between two points in time. You
can get the duration in any unit you want by accessing the Timespan''s
members.
Armin


或者在版本2008中使用选项严格和推断


\\\

Dim d1 = Date.Parse(InputBox(输入日期))

Dim d2 =现在

Dim ts = d1.Subtract(d2)

MessageBox.Show(" Difference is:& ts.TotalMinutes&" minutes")

///


Cor
Or in version 2008 with option strict and infer on

\\\
Dim d1 = Date.Parse(InputBox("Enter a date"))
Dim d2 = Now
Dim ts = d1.Subtract(d2)
MessageBox.Show("Difference is: " & ts.TotalMinutes & " minutes")
///

Cor

Dim d1,d2 as Date

Dim ts As TimeSpan


d2 =现在

ts = d1.Subtract(d2)


MsgBox(差异是:& ts.TotalMinutes&" minutes)
Dim d1, d2 As Date
Dim ts As TimeSpan

d1 = Date.Parse(InputBox("enter a date"))
d2 = Now
ts = d1.Subtract(d2)

MsgBox("Difference is: " & ts.TotalMinutes & " minutes")


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

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