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)

大卫

4

6 回答 6

2

尽量不要将方弦钉锤入圆形日期孔,这有太多方法可以打破你的木槌。Now 函数已经返回一个日期:

    Dim curDate As Date = Now.Date

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

如果您从服务器获取字符串(请不要这样做),则使用 ParseExact() 转换日期:

    Dim curDate As Date = Date.ParseExact(serverValue, "yyyyMMdd", Nothing)
于 2011-03-25T20:16:14.813 回答
1

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

于 2011-03-25T20:18:52.207 回答
1

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.

于 2011-03-25T20:18:59.490 回答
1

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.

于 2011-03-25T20:21:01.053 回答
1

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

Dim curDate As Date = DateTime.Now.Date
于 2011-03-25T20:16:27.410 回答
0

Try

Dim curDate As Date = Now
Dim srDate As Date = mid(dr(6),5,2) & "/" & right(dr(6),2) & "/" & left(dr(6),4)
于 2011-03-25T20:21:04.883 回答