VB.NET 中的 datediff 帮助

发布于 2024-10-26 15:03:45 字数 442 浏览 2 评论 0原文

嘿,我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

你没皮卡萌 2024-11-02 15:03:45

尽量不要将方形弦钉敲入圆形日期孔中,这样有太多方法会损坏您的木槌。 Now 函数已经返回一个日期:

    Dim curDate As Date = Now.Date

源代码文件顶部的 Option Strict On 可以帮助您发现此类错误。

如果您从服务器获取字符串(祈祷您没有),则使用 ParseExact() 转换日期:

    Dim curDate As Date = Date.ParseExact(serverValue, "yyyyMMdd", Nothing)

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:

    Dim curDate As Date = Now.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:

    Dim curDate As Date = Date.ParseExact(serverValue, "yyyyMMdd", Nothing)
猫腻 2024-11-02 15:03:45

为什么现在要这样格式化?你可以这样做:

Dim curDate As Date = DateTime.Now.Date

Why are you formatting Now like that? You could just do this:

Dim curDate As Date = DateTime.Now.Date
枫以 2024-11-02 15:03:45

正如其他发帖者所说,您不需要格式化 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 a string, and you're trying to assign that to a Date. 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

最终幸福 2024-11-02 15:03:45

你的问题是第一行;看来你在你的项目中关闭了Option Strict(太可惜了!),否则它根本无法编译。

Format(Now, "yyyyMMdd") 将生成以此方式格式化为字符串的当前日期。问题在于您试图将该输出(字符串)分配给 Date 变量。由于您关闭了 Option Strict,编译器会隐式指示此转换,并且运行时会尝试将非标准日期字符串转换回日期。这就是失败的地方。

尽可能少地更改代码,它应该是:

Dim curDate As Date = Now.Date
Dim srDate As Date = DateTime.ParseExact(dr(6).ToString(), "yyyyMMDD", CultureInfo.InvariantCulture).Date
Dim M As Long = DateDiff(DateInterval.Weekday, curDate, srDate)

步骤 0:严格打开选项

没有理由在关闭此选项的情况下编写新代码。如果关闭它,很可能会出现运行时错误,这些错误很容易在编译时捕获(就像这个)。这是一个应该从语言中完全消除的功能。

第 1 步:采用标准 .NET 类型和函数

虽然这不是必需的,但它将使您的代码对其他开发人员更容易阅读,并且其他开发人员的代码对您来说也更容易阅读。 FormatDateDiffNow 等都是 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 a Date variable. Because you have Option 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:

Dim curDate As Date = Now.Date
Dim srDate As Date = DateTime.ParseExact(dr(6).ToString(), "yyyyMMDD", CultureInfo.InvariantCulture).Date
Dim M As Long = DateDiff(DateInterval.Weekday, curDate, srDate)

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.

多彩岁月 2024-11-02 15:03:45

首先:

  • “MM/DD/YYYY”在世界大部分地区都不正常,只有北美才是正常的。
  • 中国使用“YYYY-MM-DD”。
  • 欧洲使用“DD/MM/YYYY”

其次,如果您正在解析已知日期格式,则可以将格式字符串传递给DateTime.Parse。就您而言,这就是您需要做的。

Firstly:

  • "MM/DD/YYYY" is not normal in most of the world, only North America.
  • China uses "YYYY-MM-DD".
  • Europe uses "DD/MM/YYYY"

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.

谢绝鈎搭 2024-11-02 15:03:45

尝试

Dim curDate As Date = Now
Dim srDate As Date = mid(dr(6),5,2) & "/" & right(dr(6),2) & "/" & left(dr(6),4)

Try

Dim curDate As Date = Now
Dim srDate As Date = mid(dr(6),5,2) & "/" & right(dr(6),2) & "/" & left(dr(6),4)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文