DateFormat format = new SimpleDateFormat(dateFormat);//dateFormat为楼主日期字符窜datetime的格式,例如“yyyy-MM-dd HH:mm:ss” Date date = format.parse(str);//str为楼主对应的datetimeSimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); sdf.setTimeZone(TimeZone.getDefault());String result=sdf.format(date);
代码贴全,你的变量datetime的定义
那是肯定的,datetime数据类型本来就精确到毫秒的.怎么可能没有时分秒.
string s = DateTime.Now.Date.ToString("yyyy-MM-dd"); string a = DateTime.Now.Year.ToString() + "-" + DateTime.Now.Month.ToString() + "-" + DateTime.Now.Day.ToString();
string ss = DateTime.Now.ToString("yyyy-MM-dd").Replace("0:00:00",""); QQ群 10649 7038
结果是string 型 ,要是转成datetime后,又会出来时分秒
dateTimePicker1.CustomFormat = "yyyy-MM-dd";
Date date = format.parse(str);//str为楼主对应的datetimeSimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
sdf.setTimeZone(TimeZone.getDefault());String result=sdf.format(date);
string a = DateTime.Now.Year.ToString() + "-" + DateTime.Now.Month.ToString() + "-" + DateTime.Now.Day.ToString();
QQ群 10649 7038
详细的可以看看c#时间转换
string strTime=string.Format("{0:D},dt)strTime输出的结果就会是这样 "XXXX年XX月XX日",你说的时分秒就没有了
dt.GetDateTimeFormats('D')[0].ToString();//2005年11月5日
dt.GetDateTimeFormats('D')[1].ToString();//2005 11 05
"2011年11月"
DateTime.Now.GetDateTimeFormats('D')[0].ToString();
"2011年11月3日"
DateTime.Now.GetDateTimeFormats('D')[1].ToString();
"2011-11-03"
这样就可以 你试一下
这样呢,试一下
从你写出这句应该是DateTime类型
datetime.ToString("yyyy-MM-dd")
然后你想得到的是一个字符串的话,你的写法应该是正确的
如果你是想得到一个不带时分秒的DateTime的话,
可以这样
datetime=datetime.Date;
++
没看清打的字
datetime 要求 必须是时间类型的 对以上错误 表示道歉
DateTime dt = DateTime.Now;
MessageBox.Show(dt.ToString("yyyy-MM-dd"));
上边两个出来的都是年月日,没有时分秒