解决方案 »

  1.   

    如果字符串长度是固定的,可以先substring截取,中间用-和:重新拼接成标准时间字符串
    如果长度不固定,没办法,转换不了
      

  2.   

    我刚试了一下,有一下方面:
    1.如果月、日是个位数(如1号到9号,1月到9月),则转换后只显示1至9,而不是01至09;
    2.如果小时也是个位,则转换后只显示1至9,而不是01至09;
    3.分、秒能显示十位的0;
            
    static void Main(string[] args)
            {
                string str = @"2015/01/03 04:02:01";
                DateTime dt = DateTime.Parse(str);
                //DateTime dt = DateTime.Now;
                Console.WriteLine("#######");
                Console.WriteLine(dt);
                Console.ReadKey();
            }
    所以,上述输出是:2015/1/3 4:02:01
    你在网上搜C# datetime,有个cnbolg把这个的用法讲的很详细,你自己看吧。我也不懂。
    http://www.cnblogs.com/ymyglhb/archive/2009/04/22/1441293.html
      

  3.   

    DateTime.ParseExact(dateTimeStr, yyyy-"MM-dd HH:mm:ss", CultureInfo.InvariantCulture);
      

  4.   

    DateTime.ParseExact(dateTimeStr, "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture);
      

  5.   

    实测,确实可以
    代码string s = "20150113144244";
    DateTime dt = DateTime.ParseExact(s, "yyyyMMddHHmmss", null);
      

  6.   

    实测,确实可以
    代码string s = "20150113144244";
    DateTime dt = DateTime.ParseExact(s, "yyyyMMddHHmmss", null);

    不对啊...
      

  7.   

    我需要的是将这样的字符串 20150113144244  转换成时间格式 2015-01-13 14:42:44
    而不是你给的那种字符串...哪吒给你的代码都已经是DateTime了,想要什么格式随便你了啊
    string s = "20150113144244";
    DateTime dt = DateTime.ParseExact(s, "yyyyMMddHHmmss", null);
    return dt.ToString("yyyy-MM-dd HH:mm:ss");
      

  8.   

    实测,确实可以
    代码string s = "20150113144244";
    DateTime dt = DateTime.ParseExact(s, "yyyyMMddHHmmss", null);

    不对啊...
    注意我给的代码,复制过去用,不要自己瞎改
      

  9.   

    我需要的是将这样的字符串 20150113144244  转换成时间格式 2015-01-13 14:42:44
    而不是你给的那种字符串...哪吒给你的代码都已经是DateTime了,想要什么格式随便你了啊
    string s = "20150113144244";
    DateTime dt = DateTime.ParseExact(s, "yyyyMMddHHmmss", null);
    return dt.ToString("yyyy-MM-dd HH:mm:ss");
    我逗了,老想着不去NEW东西然后进行转换的...
    结贴答案:     
    string s = "20150113144244";
    DateTime.ParseExact(s, "yyyyMMddHHmmss", null).ToString("yyyy-MM-dd HH:mm:ss")
      

  10.   

    label2.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
      

  11.   

    实测,确实可以
    代码string s = "20150113144244";
    DateTime dt = DateTime.ParseExact(s, "yyyyMMddHHmmss", null);

    不对啊...
    注意我给的代码,复制过去用,不要自己瞎改
    问题解决了,谢谢,我把你的代码改成
    string s = "20150113144244";
    DateTime.ParseExact(s, "yyyyMMddHHmmss", null).ToString("yyyy-MM-dd HH:mm:ss")
      

  12.   

      DateTime dt = DateTime.ParseExact("20150113144244", "yyyyMMddHHmmss", null);
      

  13.   

    用ParseExact方法将字符串转化为日期格式
     private void btn_Convert_Click(object sender, EventArgs e)
            {
                #region 针对Windows 7系统
                string s = string.Format("{0}/{1}/{2}",//得到日期字符串
                    txt_Year.Text, txt_Month.Text, txt_Day.Text);
                DateTime P_dt = DateTime.ParseExact(//将字符串转换为日期格式
                    s, "yyyy/MM/dd", null);
                #endregion
                //#region 针对Windows XP或者2003系统
                //string s = string.Format("{0}{1}{2}",//得到日期字符串
                //    txt_Year.Text, txt_Month.Text, txt_Day.Text);
                //DateTime P_dt = DateTime.ParseExact(//将字符串转换为日期格式
                //    s, "yyyyMMdd", null);
                //#endregion
                MessageBox.Show("输入的日期为: "//弹出消息对话框
                    + P_dt.ToLongDateString(), "提示!");
            }