VoodooChild
VoodooChild

Reputation: 9784

HTML input + formatting value to date

I have date as a string 16/11/2010 12:00:00 AM for example which I am inputting in

<input type="text" value="<%: Object.Instance.SomeDateAsString %>" />

Note: this can either be empty string or in 16/11/2010 12:00:00 AM format only.

How can I display it it nicely to the user as a 16-Nov-2010?

Upvotes: 0

Views: 529

Answers (2)

Brandon Montgomery
Brandon Montgomery

Reputation: 6986

<input type="text" value="<%: Object.Instance.GetFormattedDateString() %>" />

then on your object:

public String GetFormattedDateString()
{
  String returnString = String.Empty;
  DateTime parsedDateTime;
  DateTime.TryParse(this.SomeDateAsString, parsedDateTime);

  if (parsedDateTime != DateTime.MinValue)
  {
    returnString = String.Format("{0:dd-MMM-yyyy}", parsedDateTime);
  }

  return returnString;
}

Upvotes: 1

Dror
Dror

Reputation: 7303

See here for many patterns of the DateTime.ToString() Patterns.
For your specific format you need: DateTime.ToString("dd-MMM-yyyy")

Upvotes: 1

Related Questions