开发者

HTML input + formatting value to date

开发者 https://www.devze.com 2023-01-25 17:56 出处:网络
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 %>\" />

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 t开发者_StackOverflow社区he user as a 16-Nov-2010?


<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;
}


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号