使用coledatetime解析日期 [英] To parse date using coledatetime

查看:268
本文介绍了使用coledatetime解析日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我正在编写一个代码,以Tchar或字符串值的形式接受日期。我正在尝试使用COleDateTime将其转换为Date函数。

让我的输入值为2016年3月4日星期四返回无效值(1)

但如果输入输入为2016年3月4日,则代码成功执行

以下是我的代码:



Hi Guys,

I am writing a code that accept date in form of a Tchar or a string value. I am trying it to convert it into Date function using COleDateTime.
let my input value be "Thursday, 2016 March 04" returns invalid value(1)
but if i enter input as "2016 March 04" the code get successfully executed
Below is my code:

#include "stdafx.h"
#include <windows.h>
#include <atlcomtime.h> 
#include <string>
int _tmain(int argc, _TCHAR* argv[])
{
	 
	COleDateTime date;
	TCHAR strDate[100] = { L"Tuesday, 2016 March 31" };
	if (date.ParseDateTime(strDate, LOCALE_NOUSEROVERRIDE))
	{

		int day = date.GetDay();
		int  m = date.GetMonth();
		int d = date.GetDay();
		int yyyy = date.GetYear();
		printf("%d %d %d", d, m, yyyy, day);
	}
	return 0;
}









谁能告诉我什么是错的我在做什么,我应该如何纠正呢?



我尝试过:



我尝试过LANG_USER_DEFAULT而不是LOCALE_NOUSEROVERRIDE,但代码仍无效





Can anyone tell me what wrong i am doing and how should i rectify it?

What I have tried:

I have tried LANG_USER_DEFAULT instead of LOCALE_NOUSEROVERRIDE but still the code doesnt works

推荐答案

引用:

2016年3月4日星期四

Thursday, 2016 March 04

是无效日期 (3月4日是星期五)。我不会责怪 COleDateTime 对象指出它。


解析包含日期时间值的字符串总是很痛苦。



COleDateTime :: ParseDateTime [ ^ ]支持多种格式,据我所知,那些包含工作日名称的人不支持(参见上面的链接)。当字符串使用不同的语言环境时(例如,从文件或数据库中读取时),它将在大多数情况下失败。您在示例中使用的是英文日期格式。如果您的系统或用户区域设置不是英语,则解析可能会失败。然后你可以尝试传递 MAKELCID(MAKELANGID(0x09,0x01),SORT_DEFAULT)作为LCID。



如果您知道确切的格式,最好使用 scanf,_scanf_l,wscanf,_wscanf_l [ ^ ]忽略工作日名称,然后查找月份名称。



如果用户应在程序中输入日期,请​​使用DateTimePicker控件或至少带有输入掩码的编辑字段。



由于上述原因,请尽可能避免使用字符串作为日期时间值。如果需要它们(例如对于日志文件),请使用与语言环境无关的格式,如 ISO 8601 - Wikipedia,免费的百科全书 [ ^ ],可以简单地解析。
Parsing a string containing a datetime value is always a pain.

While COleDateTime::ParseDateTime[^] supports a range of formats, those containing weekday names are not supported as far as I know (see also above link). And it will fail in most cases when the string uses a different locale (e.g. when reading from a file or database). You are using an English date format in your example. If your system or user locale setting is not English, parsing may fail therefore. Then you may try passing MAKELCID(MAKELANGID(0x09, 0x01), SORT_DEFAULT) as LCID.

If you know the exact format, it would be better to use scanf, _scanf_l, wscanf, _wscanf_l[^] ignoring the weekday name and looking up the month name afterwards.

If the user should enter the date inside your program use the DateTimePicker control or at least an edit field with an input mask.

For the above reasons avoid using strings for datetime values if possible. If they are required (e.g. for log files), use a locale independant format like ISO 8601 - Wikipedia, the free encyclopedia[^] which can be simply parsed.

要定义日期,您需要年,月和日编号。日期名称不需要,并且是从日期推断出来的。

To define a date, you need Year, Month and Day number. The Day Name is not needed and is deduced from date.
引用:

2016年3月4日星期四

"Thursday, 2016 March 04"

每个人都期待一个错误,因为星期几的名字只是让事情变得复杂而不需要。

你已经知道怎么做了。

Every one expect an error with that because the Name of the day of week is just complicating things without need.
You already know how to do.


这篇关于使用coledatetime解析日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆