首先你能不能确定字符串格式就是你所说的格式呢?如果根本不能确定格式,你没有办法。
CString str = "2013-12-22 13时14分23秒前送达";
str.Replace("时",":");
str.Replace("分",":");
COleDateTime t;
t.ParseDateTime(str.Left(19));
CString ti = t.Format("%Y-%m-%d %H:%M:%S");

解决方案 »

  1.   

    你可以找到“秒”这个关键字的位置,然后去前面的这一段,再ParseDateTime
      

  2.   

    CString s(TEXT("2013-12-22 13时14分23秒前送达"));
    s.SetAt(20,TEXT('\0'));
    去掉后面几个字,应该可以吧
      

  3.   


    谢谢happyparrot及各位,我可能没说清楚啊,我意思是类似于"2013-12-22 13时14分23秒前送达"一样的字符,里边有“前送达”这样跟时间无关的字符,但是这个无关的字符串内容是不确定的,且也可能出现在时间字符串的前边
      

  4.   

    不要管无关字符串,拿到字串后,先做2个替换,将"时","分"都替换为冒号":"("秒"就不用管了),变成"2013-12-22 13:14:23秒前送达",然后用正则表达式\d{4}-\d{2}-\d{2}提取日期,\d{2}:\d{2}:\d{2}提取时间.
    不知道你用什么语言,正则表达式是通用的,用python测试一下
    >>> import re
    >>> s = '2013-12-22 13时14分23秒前送达'
    >>> ss = s.replace('时',':')
    >>> ss = ss.replace('分',':')
    >>> print ss
    2013-12-22 13:14:23秒前送达
    >>> date_res = r'\d{4}-\d{2}-\d{2}'
    >>> time_res = r'\d{2}:\d{2}\d{2}'
    >>> d = re.findall(date_res,ss)
    >>> print d
    ['2013-12-22']
    >>> time_res = r'\d{2}:\d{2}:\d{2}'
    >>> t = re.findall(time_res,ss)
    >>> print t
    ['13:14:23']
    >>> 
      

  5.   

    不要管无关字符串,拿到字串后,先做2个替换,将"时","分"都替换为冒号":"("秒"就不用管了),变成"2013-12-22 13:14:23秒前送达",然后用正则表达式\d{4}-\d{2}-\d{2}提取日期,\d{2}:\d{2}:\d{2}提取时间.
    不知道你用什么语言,正则表达式是通用的,用python测试一下
    >>> import re
    >>> s = '2013-12-22 13时14分23秒前送达'
    >>> ss = s.replace('时',':')
    >>> ss = ss.replace('分',':')
    >>> print ss
    2013-12-22 13:14:23秒前送达
    >>> date_res = r'\d{4}-\d{2}-\d{2}'
    >>> time_res = r'\d{2}:\d{2}\d{2}'
    >>> d = re.findall(date_res,ss)
    >>> print d
    ['2013-12-22']
    >>> time_res = r'\d{2}:\d{2}:\d{2}'
    >>> t = re.findall(time_res,ss)
    >>> print t
    ['13:14:23']
    >>> 
    确实,正则表达式是个好办法,结贴