首先你能不能确定字符串格式就是你所说的格式呢?如果根本不能确定格式,你没有办法。
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");
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");
s.SetAt(20,TEXT('\0'));
去掉后面几个字,应该可以吧
谢谢happyparrot及各位,我可能没说清楚啊,我意思是类似于"2013-12-22 13时14分23秒前送达"一样的字符,里边有“前送达”这样跟时间无关的字符,但是这个无关的字符串内容是不确定的,且也可能出现在时间字符串的前边
不知道你用什么语言,正则表达式是通用的,用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']
>>>
不知道你用什么语言,正则表达式是通用的,用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']
>>>
确实,正则表达式是个好办法,结贴