簡體   English   中英

在HTML文本輸入中顯示C#DateTime

[英]Display C# DateTime in HTML Text Input

因此,我要從數據庫中提取DateTime,並將其顯示在HTML文本輸入( <input type="text"> )中。

在將C#DateTime發送到Javascript時,它看起來像這樣: fooDate: {6/22/2011 2:30:00 PM} 但是,當它顯示在<input> ,它最終看起來像這樣: 2011-06-22T14:30:00

這有點煩人,因為我只關心顯示日期 為什么會發生這種轉換,有沒有辦法讓它顯示6/22/2011 2:30:00 PM位? 不使用正則表達式就可以實現嗎?

嘗試使用String.Format函數:

//Here you pull the DateTime from your db
//I show you an example with the current DateTime
DateTime myDateTime = DateTime.Now; 

string date = String.Format("{0:dd-MM-yyyy}", myDateTime); //eg: 02-12-2014

//Place the string in your textbox
this.txtYourTextbox.Text = date;

如果您不知道使用哪種格式,請參閱以下文章:

DateTime [C#]的字符串格式

在將其傳遞給JS方法之前,是否能夠將其轉換為字符串?

string Output = string.Format("{0:G}",DatabaseValue);

混合並匹配您想要的任何組合。 世界是你的牡蠣。

DateTime.Now.Day
DateTime.Now.Month
DateTime.Now.Year

約會的例子...

DateTime.Now.ToShortDateString();

將返回“ 02/12/2014”

或者您可以指定所需的格式

DateTime.Now.ToString("yyyy-MM-dd");

將返回“ 2014-12-02”

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM