I am fetching date from textbox and passing it to dateTime by passing it though a function,
BUT it gives me two different results
DateTime Fromdate1 = devTools.ParseDate(datepicker1.Text);
TextBox —>OutPut
02/09/2012 –> 2/9/2012 12:00:00 AM
30/08/2012 –> 8/30/2012 12:00:00 AM
public DateTime ParseDate(string s)
{
DateTime result;
if (!DateTime.TryParse(s, out result))
{
string[] formats = { "dd'/'MM'/'yyyy" };
result = DateTime.ParseExact(s, formats, new CultureInfo("en-US"), DateTimeStyles.None);
//result = DateTime.ParseExact(s, "dd'/'MM'/'yyyy", System.Globalization.CultureInfo.InvariantCulture);
//result = result.AddDays(1);
}
return result;
}
What is the problem here,Is there a prper way to get constant output type,also it will create problems in DB since 08/09 will be checked against 09/08 and vice versa !
One more problem in SQL :
Select convert(nchar(10),FromDate, 103) AS FromDate,convert(nchar(10),ToDate, 103) AS ToDate FROM Tbl_PropertyRoomSeasonPrices
OUTPUT :
28/08/2012 | 09/01/2012
dd/mm/yyyy | mm/dd/yyyy
(CORRECT) (WRONG)
but 103 for fromDate and 101 for toDate makes it both same,Why the variations ??
It seems to me that the “problem” here is just how you’re interpreting the output. Your method returns a
DateTime– not a string – so when you’ve written. “02/09/2012 –> 2/9/2012 12:00:00 AM” you presumably mean that an input of string “02/09/2012” gives you a resultDateTimewhich happens to have a string representation of “2/9/2012 12:00:00 AM” in whatever way you’re converting it to a string, e.g. by looking in the debugger. That will use the current culture and the default format – but that’s not part of theDateTimevalue itself.Look at the properties of the
DateTimeand you’ll see it’s been parsed correctly.No it won’t. Not if you’re doing things properly – using parameterized SQL and specifying the
DateTimevalue for the property. It could well give you problems if you’re including the value directly in your SQL, but that’s a bad idea anyway.