Question

I want to convert the date into something different. I give input date, it will convert into that format.

My code

 var dt = erd.StartDate.Value.ToShortDateString();

 var format = String.Format("{MMM/D/yyyy}", dt);

Here it is showing an error like input string is not correct format.

Help me to find out the issue?

Was it helpful?

Solution

Simply rewrite it like this (I'm assuming the erd.StartDate.Value datatype is datetime)

string result = erd.StartDate.Value.ToString("MMM/dd/yyyy");

or using "D" is equivalent to "dddd, MMMM d, yyyy"

string result = erd.StartDate.Value.ToString("D");

OTHER TIPS

  1. you need to use small d for representing Date.
  2. you should not convert the Date into String before applying the custom Format.

Try This:

var dt = erd.StartDate.Value;
var format =dt.ToString("MMM/d/yyyy");

To solve your error, try the below code:

var dt = erd.StartDate.Value;

var format = String.Format("{0:MMMM/dd/yyyy}", dt);

You need to specify the parameter index to use. In the above code, we are telling String.Format to use parameter 0 to be formatted as a DateTime.

DateTime formatting has many types. Please see here for the different things you can do.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top