Javascript 在HTML文本输入中显示C#DateTime
因此,我从数据库中提取日期时间,并希望在HTML文本输入中显示它(Javascript 在HTML文本输入中显示C#DateTime,javascript,c#,html,datetime,Javascript,C#,Html,Datetime,因此,我从数据库中提取日期时间,并希望在HTML文本输入中显示它() 当C#DateTime被发送到Javascript时,它看起来是这样的:fooDate:{6/22/2011 2:30:00 PM}。但是,当它显示在中时,它最终看起来是这样的:2011-06-22T14:30:00 这有点烦人,因为我只关心显示日期。为什么会发生这种转换,有没有办法让它显示6/22/2011 2:30:00 PM位?不使用正则表达式可以实现这一点吗?尝试使用String.Format函数: //Here yo
)
当C#DateTime被发送到Javascript时,它看起来是这样的:fooDate:{6/22/2011 2:30:00 PM}
。但是,当它显示在
中时,它最终看起来是这样的: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;
如果您不知道使用哪种格式,请参阅以下文章:
尝试使用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;
如果您不知道使用哪种格式,请参阅以下文章:
在将其传递给JS方法之前,是否能够将其转换为字符串
string Output = string.Format("{0:G}",DatabaseValue);
在将其传递给JS方法之前,是否能够将其转换为字符串
string Output = string.Format("{0:G}",DatabaseValue);
混合搭配你想要的任何组合。世界就是你的牡蛎
DateTime.Now.Day
DateTime.Now.Month
DateTime.Now.Year
混合搭配你想要的任何组合。世界就是你的牡蛎
DateTime.Now.Day
DateTime.Now.Month
DateTime.Now.Year
刚开始约会的例子
DateTime.Now.ToShortDateString();
将返回“2014年12月2日”
也可以指定所需的格式
DateTime.Now.ToString("yyyy-MM-dd");
将返回“2014-12-02”仅获取日期的示例
DateTime.Now.ToShortDateString();
将返回“2014年12月2日”
也可以指定所需的格式
DateTime.Now.ToString("yyyy-MM-dd");
将返回“2014-12-02”如何在输入中显示日期?如何在输入中显示日期?