Convert mm / dd / yyyy to yyyymmdd (VB.NET)

Is there a way to convert the format date: dd / mm / yyyy to yyyymmdd format? For example: from 07/25/2011 to 20110725? in vb.net?

+6
source share
3 answers

Dates themselves do not have formats. You can parse a string in DateTime by parsing it using the dd/MM/yyyy format, and then convert it to a string using the 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.

+9
source
  CDate(Datetext).ToString("yyyyMMdd") 
+3
source

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

DaTeTime.ParseExact

0
source

Source: https://habr.com/ru/post/893771/


All Articles