我正在尝试格式化以下值01/08/2020
Dim value1 as string = '01/08/2020'
Response.Write(Format(CDate(value1),"yyyy-MMM-dd"))结果为2020-1月-08,而不是2020-8月-01。
发布于 2020-08-25 15:37:24
有一种更容易格式化日期的方法。试试这个:
Response.Write(DateTime.ParseExact(value1, "dd/MM/yyyy", null).ToString("yyyy-MMM-dd"))有关各种可能的日期格式,请参见https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings。
发布于 2020-08-25 17:25:33
出于谨慎我会这么做
Dim value1 As String = "1/8/2020" '
Dim d As Date
'https://learn.microsoft.com/en-us/dotnet/api/system.datetime.tryparseexact?view=netcore-3.1#System_DateTime_TryParseExact_System_String_System_String___System_IFormatProvider_System_Globalization_DateTimeStyles_System_DateTime__
If DateTime.TryParseExact(value1, "d/M/yyyy", Nothing, Globalization.DateTimeStyles.None, d) Then
Dim s As String = d.ToString("yyyy-MMM-dd")
Response.Write(s)
Else
Stop 'error placeholder
End If在解析出现错误时,您需要添加停止的代码。
https://stackoverflow.com/questions/63582278
复制相似问题