-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,804
  • 11
  • 54
  • 93

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,716
  • 9
  • 69
  • 101
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,427
  • 3
  • 42
  • 60