Convert DateTime to a string with the format YYYYMMDD

I have birth dates stored as a date in SQL Server 2008, for example:

2010-04-25 00:00:00.000 

What is the best way to use C # to convert and format to a string with the format YYYYMMDD ?

In the end, I only need a line:

 20100425 

Any help is much appreciated!

+4
source share
5 answers
 date.ToString("yyyyMMdd"); 

It should be what you need.

+10
source

You need to get this value in a DateTime object, and then you can use its ToString () function as follows:

 .ToString("yyyyMMdd"); 
+2
source

Can you retrieve data from a database as a DateTime (.NET) object? If so, you can use DateTime instancename.ToString ("yyyyMMdd")

If you have not reached this stage, there are quite a few different ways to get data. This is a whole Google search in itself ...

+1
source

You simply format the date with a custom format string:

 string formatted = theDate.ToString("yyyyMMdd"); 

Note that a date does not have a format at all when it is stored as a date-time in the database. This is just a point in time; it does not have a specific textual representation until it is specially created from the date.

+1
source

Use the .ToString () method for the date object and pass in the desired format.

0
source

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


All Articles