-4

I have a method that takes a parameter of the type DateTime?

I need to convert what I receive to a string of the format - yyyy-mm-dd

What is the cleanest optimal way to achieve this?

GilliVilla
  • 4,998
  • 11
  • 55
  • 96

2 Answers2

2

Assuming dateParameter if of type DateTime, just like you said:

dateParameter.ToString("yyyy-mm-dd");

EDIT:

Since you edited your post to nullable DateTime, here's an edit:

dateParameter.Value.ToString("yyyy-mm-dd");
B.K.
  • 9,982
  • 10
  • 73
  • 105
1

You can use DateTime.ToString ..below is the sample code :-

datePassed.ToString("yyyy-mm-dd");

And for nullable DateTime :-

datePassed.Value.ToString("yyyy-mm-dd");
Neel
  • 11,625
  • 3
  • 43
  • 61