Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert DateTime to string with format YYYYMMDD

I have birth dates stored as datetime in SQL Server 2008 like so:

2010-04-25 00:00:00.000

What is the best way, using C#, to convert and format this into a string with a YYYYMMDD format?

In the end, all I need is a string like:

20100425

Any help is greatly appreciated!

like image 646
Sesame Avatar asked Dec 06 '22 02:12

Sesame


2 Answers

date.ToString("yyyyMMdd");

Should be what you need.

like image 82
Matthew Abbott Avatar answered Dec 22 '22 02:12

Matthew Abbott


You need to get that value into a DateTime object and then you can use it's ToString() function like so:

.ToString("yyyyMMdd");
like image 26
Iain Ward Avatar answered Dec 22 '22 03:12

Iain Ward