VB.NET 中的自定义日期格式问题 [英] Issue with Custom Date Format in VB.NET

查看:42
本文介绍了VB.NET 中的自定义日期格式问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!

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

但他们都没有为我工作.

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

代码:

公共函数 FirstDayOfMonth(ByVal sourceDate As DateTime) As DateTime返回新日期时间(sourceDate.Year, sourceDate.Month, 1)结束函数公共函数 LastDayOfMonth(ByVal sourceDate As DateTime) As DateTimeDim lastDay As DateTime = New DateTime(sourceDate.Year, sourceDate.Month, 1)返回 lastDay.AddMonths(1).AddDays(-1)结束函数

这是输出

3/1/2018 &2018/3/31

示例:

我第一次尝试基于 Source#1

Dim startDayMonth As StringDim endDayMonth As StringstartDayMonth = startDayMonth.ToString("dd")endDayMonth = endDayMonth.ToString("dd")MsgBox(startDayMonth & " " & endDayMonth)

输出:

TIMELOG.exe 中发生类型为System.InvalidCastException"的未处理异常附加信息:无法将System.String"类型的对象转换为System.IFormatProvider"类型.

我第二次尝试基于 Source#2

Dim startDayMonth As DateTimeDim endDayMonth As DateTimeDim dateFormat As String日期格式 = "%d"startDayMonth = FirstDayOfMonth(现在)endDayMonth = LastDayOfMonth(现在)startDayMonth = startDayMonth.ToString(dateFormat)endDayMonth = endDayMonth.ToString(dateFormat)

输出:

Microsoft.VisualBasic.dll 中发生类型为System.InvalidCastException"的未处理异常附加信息:从字符串1"到类型日期"的转换无效.

自己的方法#1

Dim startDayMonth As DateTimeDim endDayMonth As DateTimeDim dateFormat As StringdateFormat = "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 DateTimeDim endDayMonth As DateTimeDim dateFormat As StringdateFormat = "%yyyy-MM-dd"startDayMonth = Convert.ToString(FirstDayOfMonth(Now))endDayMonth = Convert.ToString(LastDayOfMonth(Now))startDayMonth = startDayMonth.ToString(dateFormat)endDayMonth = endDayMonth.ToString(dateFormat)

输出

Microsoft.VisualBasic.dll 中发生类型为System.InvalidCastException"的未处理异常附加信息:从字符串182018-03-01"到类型日期"的转换无效.

解决方案

在 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 不允许从字符串"到日期"的隐式转换.

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

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

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

Good day everyone!

I have a problem about custom date format in VB.NET and I already tried to use these codes:

but none of them are working for me.

First, this the source code where I get the first and last day of the month

Code:

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

This is the output

3/1/2018 & 3/31/2018

Example:

My first attempt based on Source# 1

Dim startDayMonth As String
Dim endDayMonth As String

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

MsgBox(startDayMonth & " " & endDayMonth)

OUTPUT:

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'.

My second attempt based on 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)

OUTPUT:

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

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

Own Method #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)

OUTPUT

3/1/2018 3/31/2018

Own Method #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)

OUTPUT

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.

解决方案

You should set Option Strict On always, 100% of the time when coding in VB.NET.

Source #1

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

I get a compile-time error:

BC30512 Option Strict On disallows implicit conversions from 'String' to 'IFormatProvider'.

Without Option Strict On I get a run-time error:

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

Compile-time errors are infinitely better than run-time errors. It's impossible to deploy compile-time errors to customers.

Source #2

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

BC30512 Option Strict On disallows implicit conversions from 'String' to 'Date'.

The same applies with own method #1 and own method #2.

You would not have even asked this question had you have Option Strict On.

Please turn it on. Then your compiler can tell you where you are going wrong.

这篇关于VB.NET 中的自定义日期格式问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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