Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C++
#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;
}




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
Posted
Updated 31-Mar-16 5:51am
v2
Comments
jeron1 31-Mar-16 11:08am    
Can't you just modify the string to get rid of the "Thursday," portion?

Well
Quote:
Thursday, 2016 March 04
is an invalid date (04 March was Friday). I wouldn't blame the COleDateTime object for pointing it out.
 
Share this answer
 
Comments
jeron1 31-Mar-16 11:24am    
Ha! Ha! Good spot! note to self, look for the obvious!
CPallini 31-Mar-16 11:39am    
Thank you. :-)
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.
 
Share this answer
 
To define a date, you need Year, Month and Day number. The Day Name is not needed and is deduced from date.
Quote:
"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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900