Convert datetime "DD / MM / YYYY" to a string "YYMMDD" in C #

2

I have the value of a date in a DateEdit which the default value is DD/MM/YYYY and I need to convert it to a string format YYMMDD to show it in a TextBox since it will define the name of a task on that date.

I have this code using the ParseExact() method:

DateTime fecha = DateTime.ParseExact(edtStartDate.Text, new[] { "YYYYMMDD", "YYMMDD" }, CultureInfo.InvariantCulture, DateTimeStyles.None );
tbSubject.Text = fecha.ToString("YYMMDD"); 

But it throws an exception that does not recognize it as a valid string and I can not find what I'm doing wrong:

  

String was not recognized as a valid DateTime.

    
asked by Pablo Matias 09.08.2017 в 16:30
source

2 answers

3

Operators% co_of% of ToString() can be found at this page .

The problem you have, is that you are putting the text wrong in DateTime .

If you look at the page I have given you, the days are indicated with ToString() lowercase%, the months are indicated with the% co_of uppercase%, but again, the years are indicated with the d lowercase%.

Try to modify those parts and see if the error is solved.

    
answered by 09.08.2017 / 16:37
source
2

Assuming you want the format in YearMyDay (yyyyMMdd) it would only be to convert the date with ToString() in the following way: fecha.ToString("yyyyMMdd") ; This is explained by the colleague Marc in the previous answer.

Greetings

    
answered by 09.08.2017 в 16:46