7

有什么方法可以将日期格式:dd/mm/yyyy 转换为 yyyymmdd 格式?例如:从 25/07/2011 到 20110725?在 VB.NET 中?

4

4 回答 4

13

Dates themselves don't have formats inherently. You can parse a string into a DateTime by parsing it with dd/MM/yyyy format and then convert that into a string using yyyyMMdd format:

DateTime date = DateTime.ParseExact(text, "dd/MM/yyyy",
                                    CultureInfo.InvariantCulture);

string reformatted = date.ToString("yyyyMMdd", CultureInfo.InvariantCulture);

Or in VB:

Dim date as DateTime = DateTime.ParseExact(text, "dd/MM/yyyy", CultureInfo.InvariantCulture)
Dim reformatted as String = date.ToString("yyyyMMdd", CultureInfo.InvariantCulture)

(And make sure you have an import for System.Globalization.)

However, ideally you should keep it as a DateTime (or similar) for as long as possible.

于 2011-07-28T12:25:20.983 回答
5
 CDate(Datetext).ToString("yyyyMMdd")
于 2014-12-16T06:58:21.957 回答
0

Use the DateTime.ParseExact method to parse the date, then use DateTimeObj.ToString("yyyyMMdd").

DaTeTime.ParseExact

于 2011-07-28T12:25:45.693 回答
0
Public Function DateFormateYYYYMMDD(ByVal Dtp As DateTimePicker) As String

   Try
      Dim StrDate, StrYear, StrMonth, StrDay As String
      StrDate = FormatDateTime(Dtp.Value, DateFormat.ShortDate)
      StrMonth = Month(Dtp.Value)
      StrDay = Convert.ToString(Dtp.Value.Day)
      StrYear = Year(Dtp.Value)
      StrDate = StrYear + "-" + StrMonth + "-" + StrDay

      Return StrDate
   Catch ex As Exception

   End Try
End Function

此函数可用于将日期时间选择器值格式转换为 yyyyMMdd

于 2021-07-09T07:21:56.947 回答