转化为日期问题,高手指教 TextBox3.Text="2010-10-10"要把他转化为日期型的2010-10-10怎么转呀,不要带有小时和秒的 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 如果 你要 在页面中显示为 "2010-10-10" 就不需要转换如果要保持到数据库 是 "2010-10-10" 那么在数据库 设置字段类型为date string.Format("{0:yyyy/MM/dd}",你的日期); string.Format("{0:yyyy-MM-dd}",你的日期); 可写函数,解决经常用到的方法不知道能否转时间的,可先用该函数。 public static DateTime ToTime(Object objTime, DateTime targetTime) { if (objTime == null) { return targetTime; } try { return Convert.ToDateTime(objTime); } catch { return targetTime; } }当知道是时间。可随便格式。(注:你要的效果"2010-10-10".ToString("yyyy-MM-dd"))以下为参考 dt.GetDateTimeFormats('t')[0].ToString();//14:06 dt.GetDateTimeFormats('y')[0].ToString();//2005年11月 dt.GetDateTimeFormats('D')[0].ToString();//2005年11月5日 dt.GetDateTimeFormats('D')[1].ToString();//2005 11 05 dt.GetDateTimeFormats('D')[2].ToString();//星期六 2005 11 05 dt.GetDateTimeFormats('D')[3].ToString();//星期六 2005年11月5日 dt.GetDateTimeFormats('M')[0].ToString();//11月5日 dt.GetDateTimeFormats('f')[0].ToString();//2005年11月5日 14:06 dt.GetDateTimeFormats('g')[0].ToString();//2005-11-5 14:06 dt.GetDateTimeFormats('r')[0].ToString();//Sat, 05 Nov 2005 14:06:25 GMT string.Format("{0:d}",dt);//2005-11-5 string.Format("{0:D}",dt);//2005年11月5日 string.Format("{0:f}",dt);//2005年11月5日 14:23 string.Format("{0:F}",dt);//2005年11月5日 14:23:23 string.Format("{0:g}",dt);//2005-11-5 14:23 string.Format("{0:G}",dt);//2005-11-5 14:23:23 string.Format("{0:M}",dt);//11月5日 string.Format("{0:R}",dt);//Sat, 05 Nov 2005 14:23:23 GMT string.Format("{0:s}",dt);//2005-11-05T14:23:23 string.Format("{0:t}",dt);//14:23 string.Format("{0:T}",dt);//14:23:23 string.Format("{0:u}",dt);//2005-11-05 14:23:23Z string.Format("{0:U}",dt);//2005年11月5日 6:23:23 string.Format("{0:Y}",dt);//2005年11月 string.Format("{0}",dt);//2005-11-5 14:23:23 string.Format("{0:yyyyMMddHHmmssffff}",dt); 转为日期格式:DateTime.Parse(TextBox3.Text);再转为其他格式的日期字符:DateTime.Parse(TextBox3.Text).ToString("yyyy-MM-dd"); 这个是正解。日期格式应该就是年月日时分秒的记录。所以说要没时分秒的应该不行没时分秒的还是要转到string吧。所以你的转换之后应该是: 2010-10-10 00:00:00其实你使用的时候再格式化一下datetime就OK了 我现在是要写进数据库,数据库是access,类型是时间/日期,但是怎么写进去的都是数据类型不对。唉。string sqSql = "INSERT INTO 事件 (学号,时间,操行编号,学期编号,标题,内容,是否处理,管理员编号) VALUES ('" + Session["userid"] + "','" +DateTime.TryParse(TextBox3.Text,out dt) + "'," + sxbh + "," + DropDownList1.SelectedIndex + ",'" + TextBox1.Text.Trim() + "','" + TextBox2.Text.Trim() + "','0','')";不知道错在哪 Convert.ToDateTime("2010-10-10").ToShortDateString(); ConvertToDateTime()ToDateTime("yyyy-MM-dd") 在网页中打开一个窗体怎么做 问一个牛X的问题,小弟望大虾指教 做图片自定义控件,调用的时候只显示图片,别的什么都没有了 lable控件显示文档 javascript问题? 请教大家一个相对路径的问题`~~~~~~~~~~~~~~~~~~~~~~~~~~~ .ascx文件怎么知道它的.cs文件? 在线等待IE窗口打开问题。 如何发布网站 在javascript脚本中,一行写太多,要分多行写,用什么连接符号? 菜鸟高分求一条简单的查询语句 .net 中 httpHandlers 的问题
public static DateTime ToTime(Object objTime, DateTime targetTime)
{ if (objTime == null)
{
return targetTime;
}
try
{
return Convert.ToDateTime(objTime);
}
catch
{
return targetTime;
}
}当知道是时间。可随便格式。(注:你要的效果"2010-10-10".ToString("yyyy-MM-dd"))以下为参考
dt.GetDateTimeFormats('t')[0].ToString();//14:06
dt.GetDateTimeFormats('y')[0].ToString();//2005年11月
dt.GetDateTimeFormats('D')[0].ToString();//2005年11月5日
dt.GetDateTimeFormats('D')[1].ToString();//2005 11 05
dt.GetDateTimeFormats('D')[2].ToString();//星期六 2005 11 05
dt.GetDateTimeFormats('D')[3].ToString();//星期六 2005年11月5日
dt.GetDateTimeFormats('M')[0].ToString();//11月5日
dt.GetDateTimeFormats('f')[0].ToString();//2005年11月5日 14:06
dt.GetDateTimeFormats('g')[0].ToString();//2005-11-5 14:06
dt.GetDateTimeFormats('r')[0].ToString();//Sat, 05 Nov 2005 14:06:25 GMT
string.Format("{0:d}",dt);//2005-11-5
string.Format("{0:D}",dt);//2005年11月5日
string.Format("{0:f}",dt);//2005年11月5日 14:23
string.Format("{0:F}",dt);//2005年11月5日 14:23:23
string.Format("{0:g}",dt);//2005-11-5 14:23
string.Format("{0:G}",dt);//2005-11-5 14:23:23
string.Format("{0:M}",dt);//11月5日
string.Format("{0:R}",dt);//Sat, 05 Nov 2005 14:23:23 GMT
string.Format("{0:s}",dt);//2005-11-05T14:23:23
string.Format("{0:t}",dt);//14:23
string.Format("{0:T}",dt);//14:23:23
string.Format("{0:u}",dt);//2005-11-05 14:23:23Z
string.Format("{0:U}",dt);//2005年11月5日 6:23:23
string.Format("{0:Y}",dt);//2005年11月
string.Format("{0}",dt);//2005-11-5 14:23:23
string.Format("{0:yyyyMMddHHmmssffff}",dt);
再转为其他格式的日期字符:DateTime.Parse(TextBox3.Text).ToString("yyyy-MM-dd");
日期格式应该就是年月日时分秒的记录。所以说要没时分秒的应该不行
没时分秒的还是要转到string吧。
所以你的转换之后应该是: 2010-10-10 00:00:00
其实你使用的时候再格式化一下datetime就OK了
string sqSql = "INSERT INTO 事件 (学号,时间,操行编号,学期编号,标题,内容,是否处理,管理员编号) VALUES ('" + Session["userid"] + "','" +DateTime.TryParse(TextBox3.Text,out dt) + "'," + sxbh + "," + DropDownList1.SelectedIndex + ",'" + TextBox1.Text.Trim() + "','" + TextBox2.Text.Trim() + "','0','')";不知道错在哪
ToDateTime("yyyy-MM-dd")