VB.NET 中的 datediff 帮助
嘿,我有 2 个日期,我需要看看不同的日子。
问题是服务器日期不是正常的 MM/DD/YYYY 格式。它的格式为YYYYMMDD。
我已尝试以下操作:
Dim curDate As Date = Format(Now, "yyyyMMdd")
Dim srDate As Date = dr(6)
Dim M As Long = DateDiff(DateInterval.Weekday, curDate, srDate)
curDate 存在以下错误:
Conversion from string "20110325" to type 'Date' is not valid.
任何帮助都会很棒! :o)
大卫
Hey all i have 2 dates that i need to see the days that are different.
Problem being is that the server date is not in the normal MM/DD/YYYY format. It is in the format YYYYMMDD.
I've tried the following:
Dim curDate As Date = Format(Now, "yyyyMMdd")
Dim srDate As Date = dr(6)
Dim M As Long = DateDiff(DateInterval.Weekday, curDate, srDate)
The curDate has the error of:
Conversion from string "20110325" to type 'Date' is not valid.
Any help would be great! :o)
David
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
尽量不要将方形弦钉敲入圆形日期孔中,这样有太多方法会损坏您的木槌。 Now 函数已经返回一个日期:
源代码文件顶部的 Option Strict On 可以帮助您发现此类错误。
如果您从服务器获取字符串(祈祷您没有),则使用 ParseExact() 转换日期:
Try not to hammer a square string peg into a round date hole, that just has way too many ways to break your mallet. The Now function already returns a date:
Option Strict On at the top of the source code file helps you find these kinds of mistakes.
If you get the string from the server (pray you don't) then use ParseExact() to convert the date:
为什么现在要这样格式化?你可以这样做:
Why are you formatting Now like that? You could just do this:
正如其他发帖者所说,您不需要格式化 DateTime.Now。
但这里还有其他问题:
Format
返回一个string
,而您试图将其分配给Date
。它试图隐式转换字符串,但失败了。将来,当您确实将“yyyyMMdd”之类的日期字符串转换为 DateTime 类型时,请使用
DateTime.Parse
As the other posters have said, you don't need to format DateTime.Now.
But there's something else going wrong here:
Format
returns astring
, and you're trying to assign that to aDate
. It's trying to implicitly convert a string, and failing.In future, when you do have a date-string like "yyyyMMdd" to turn into a DateTime type, use
DateTime.Parse
你的问题是第一行;看来你在你的项目中关闭了
Option Strict
(太可惜了!),否则它根本无法编译。Format(Now, "yyyyMMdd")
将生成以此方式格式化为字符串的当前日期。问题在于您试图将该输出(字符串)分配给Date
变量。由于您关闭了 Option Strict,编译器会隐式指示此转换,并且运行时会尝试将非标准日期字符串转换回日期。这就是失败的地方。尽可能少地更改代码,它应该是:
步骤 0:严格打开选项
没有理由在关闭此选项的情况下编写新代码。如果关闭它,很可能会出现运行时错误,这些错误很容易在编译时捕获(就像这个)。这是一个应该从语言中完全消除的功能。
第 1 步:采用标准 .NET 类型和函数
虽然这不是必需的,但它将使您的代码对其他开发人员更容易阅读,并且其他开发人员的代码对您来说也更容易阅读。
Format
、DateDiff
、Now
等都是 VB 特定的函数,它们的存在主要是为了使经典 VB6 应用程序更容易移植到.NET。除非有特殊原因需要使用特定于语言的版本,否则最好使用标准 .NET 函数。Your problem is the first line; it seems you have
Option Strict
off in your project (FOR SHAME!), as it would otherwise not compile at all.Format(Now, "yyyyMMdd")
will produce the current date formatted in that manner as a string. The trouble is that you're attempting to assign that output (the string) to aDate
variable. Because you haveOption Strict
off, the compiler indicates this conversion implicitly, and the runtime is attempting to convert your non-standard date string back into a date. This is what's failing.Changing as little as possible about your code, it should read:
Step 0: TURN OPTION STRICT ON
There's no reason that new code should be written with this option turned off. There's too much potential for runtime errors that are easily caught at compile time (like this one) with it off. It's a feature that should be banished from the language entirely.
Step 1: Adopt standard .NET types and functions
While this isn't required, it will make your code more readable to other developers and other developers' code more readable to you. Things like
Format
,DateDiff
,Now
, etc. are all VB-specific functions that exist primarily to make it easier for classic VB6 applications to be ported over to .NET. Unless there's a particular reason to use the language-specific versions, it's a good idea to use standard .NET functions instead.首先:
其次,如果您正在解析已知日期格式,则可以将格式字符串传递给
DateTime.Parse
。就您而言,这就是您需要做的。Firstly:
Secondly, if you are parsing a known date format, you can pass a format string to
DateTime.Parse
. In your case that is what you need to do.尝试
Try