9

Is there any way I can convert a date of format: dd/mm/yyyy to yyyymmdd format? For example from : 25/07/2011 to 20110725? in VB.NET?

l3_08
  • 191
  • 1
  • 4
  • 13

4 Answers4

15

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Jon, Just curious about this one. Why can't he use `String.Format` method? – reggie Jul 28 '11 at 12:36
  • 1
    @reggie: For the second line? He could, but it would be more work IMO. Why specify a compound string format specifier when you only want to format a single value? – Jon Skeet Jul 28 '11 at 12:41
  • Thanks for the tip, the second line is what I missed! – l3_08 Jul 28 '11 at 13:09
  • I'm not at all accomplished in vb.net, so please don't take this as a criticism because it is just a question..could be something I'm missing. I understand a little bit about what the whole Culture piece refers to from a semantic standpoint, but from a syntactical standpoint it appears to be completely ... Not accepted, when I try to type it into the SSIS IDE for .net. What am I missing? – Isaac May 29 '15 at 20:51
  • @Isaac: I don't know anything about the SSIS IDE, but the original answer was only in C#, not VB. I've provided a VB version as well now. Make sure you've imported the `System.Globalization` namespace. – Jon Skeet May 30 '15 at 04:39
  • Ahh, ok. I was sort of thinking that. Thank you – Isaac May 30 '15 at 14:23
7
 CDate(Datetext).ToString("yyyyMMdd")
Baby Groot
  • 4,637
  • 39
  • 52
  • 71
Ritz Arlekar
  • 375
  • 1
  • 4
  • 20
  • 2
    This answer could fail depending on the culture. That's why Jon's answer is better than this one. For example, how would decide if 10/12/2017 is 10th December or 12th October without the ParseExact? – Nizam Dec 17 '17 at 07:07
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

this function can be used to convert datetime picker value format to yyyyMMdd

Luis Paulo Pinto
  • 5,578
  • 4
  • 21
  • 35
0

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

DaTeTime.ParseExact

Vincent Van Den Berghe
  • 5,425
  • 2
  • 31
  • 40