VB.NET中的自定义日期格式问题

时间:2018-03-08 01:56:48

标签: vb.net

大家好日子!

我在VB.NET中遇到自定义日期格式的问题,我已经尝试使用这些代码:

但他们都没有为我工作。

首先,这是我获得该月第一天和最后一天的源代码

代码:

Public Function FirstDayOfMonth(ByVal sourceDate As DateTime) As DateTime    
    Return New DateTime(sourceDate.Year, sourceDate.Month, 1)
End Function

Public Function LastDayOfMonth(ByVal sourceDate As DateTime) As DateTime
    Dim lastDay As DateTime = New DateTime(sourceDate.Year, sourceDate.Month, 1)
    Return lastDay.AddMonths(1).AddDays(-1)
End Function

这是输出

3/1/2018 & 3/31/2018

示例:

我的第一次尝试基于Source#1

Dim startDayMonth As String
Dim endDayMonth As String

startDayMonth = startDayMonth.ToString("dd")
endDayMonth = endDayMonth.ToString("dd")

MsgBox(startDayMonth & " " & endDayMonth)

输出:

An unhandled exception of type 'System.InvalidCastException' occurred in TIMELOG.exe

Additional information: Unable to cast object of type 'System.String' to type 'System.IFormatProvider'.

我的第二次尝试基于Source#2

Dim startDayMonth As DateTime
Dim endDayMonth As DateTime
Dim dateFormat As String

dateFormat = "%d"

startDayMonth = FirstDayOfMonth(Now)
endDayMonth = LastDayOfMonth(Now)

startDayMonth = startDayMonth.ToString(dateFormat)
endDayMonth = endDayMonth.ToString(dateFormat)

输出:

An unhandled exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll

Additional information: Conversion from string "1" to type 'Date' is not valid.

自己的方法#1

Dim startDayMonth As DateTime
Dim endDayMonth As DateTime
Dim dateFormat As String

dateFormat = "yyyy-MM-dd"

startDayMonth = Convert.ToString(FirstDayOfMonth(Now))
endDayMonth = Convert.ToString(LastDayOfMonth(Now))

startDayMonth = startDayMonth.ToString(dateFormat)
endDayMonth = endDayMonth.ToString(dateFormat)

输出

3/1/2018 3/31/2018

自己的方法#2

Dim startDayMonth As DateTime
Dim endDayMonth As DateTime
Dim dateFormat As String

dateFormat = "%yyyy-MM-dd"

startDayMonth = Convert.ToString(FirstDayOfMonth(Now))
endDayMonth = Convert.ToString(LastDayOfMonth(Now))

startDayMonth = startDayMonth.ToString(dateFormat)
endDayMonth = endDayMonth.ToString(dateFormat)

输出

An unhandled exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll

Additional information: Conversion from string "182018-03-01" to type 'Date' is not valid.

2 个答案:

答案 0 :(得分:0)

更好地关注您在什么情况下使用的数据类型。字符串不是DateTime值,DateTime值不是字符串。您需要使用正确类型声明的变量来保存每种值。

FirstDayOfMonth()LastDayOfMonth()个函数使用DateTime个值。这很好。但是,DateTime 始终 对其数据使用二进制格式,而这些格式不是人类可读的。你无法改变这一点,但这没关系;您可以轻松地将值转换为您想要的格式的字符串。您只需使用不同的变量来存储声明为使用string类型的字符串值:

Dim format As String = "%d"
Dim startDayMonth As DateTime = FirstDayOfMonth(Now)
Dim endDayMonth As DateTime = LastDayOfMonth(Now)
Dim startDayMonthFormatted As String = startDayMonth.ToString(format)
Dim lastDayMonthFormatted As String = lastDayMonth.ToString(format)

答案 1 :(得分:0)

在VB.NET中编码时,您应该始终将Option Strict On 设置为100%的时间

来源#1

startDayMonth = startDayMonth.ToString("dd")
endDayMonth = endDayMonth.ToString("dd")

我收到编译时错误

  

BC30512 Option Strict On禁止从'String'到'IFormatProvider'的隐式转换。

没有Option Strict On我收到运行时错误

  

InvalidCastException:无法将类型为“System.String”的对象强制转换为“System.IFormatProvider”。

编译时错误比运行时错误无限好。向客户部署编译时错误是不可能的。

来源#2

startDayMonth = startDayMonth.ToString(dateFormat)
endDayMonth = endDayMonth.ToString(dateFormat)
  

BC30512 Option Strict On禁止从'String'到'Date'的隐式转换。

同样适用于自己的方法#1 自己的方法#2

如果你有Option Strict On,你甚至不会问这个问题。

请打开它。然后你的编译器会告诉你哪里出错了。