DateTime Hell

  • Thread starter Thread starter Franck Diastein
  • Start date Start date
F

Franck Diastein

Hi, I'm soon finishing my first app, and I learned a lot :-)

I'm still having trouble with dates...

When I pull a date from SQL Server, I store it in a DateTime variable
and when I want to display it I do this : _date.ToString("dd/MM/yyyy")
and this works fine...

But how do I asign to my DateTime _date variable a string entered by
user aka 15/12/2004 ?

I tried some code floating around with no success...

So, what do I need to perform something like this:

_date = SOME_FUNCTION("15/12/2004");

TIA
 
Franck said:
Hi, I'm soon finishing my first app, and I learned a lot :-)

I'm still having trouble with dates...

When I pull a date from SQL Server, I store it in a DateTime variable
and when I want to display it I do this : _date.ToString("dd/MM/yyyy")
and this works fine...

But how do I asign to my DateTime _date variable a string entered by
user aka 15/12/2004 ?

I tried some code floating around with no success...

So, what do I need to perform something like this:

_date = SOME_FUNCTION("15/12/2004");

TIA

Look at DateTime.Parse(string, IFormatProvider)
 
Try this:
DateTime d1 = new DateTime(2004, 10, 15); // year, month, day
// or
DateTime d2 = DateTime.Parse("10/15/2004");


Hope this helps mate.

Cheers,

Shariq Khan
(e-mail address removed)
 
I've been testing and it's works the way you tell me, but I still don't
know how to do it without specifiying time... :-(
 
Franck said:
I've been testing and it's works the way you tell me, but I still don't
know how to do it without specifiying time... :-(

string [] dateFormats = { "dd/MM/yyyy" };
DateTime.ParseStringExact(dateString, dateFormats, ...);
 
I can't beleive it !!!

In fact it's ASP.NET...

Finally, I have adapted a VB code to do this:

using System.Globalization;

DateTimeFormatInfo _Format = getDateTimeFormatInfo("fr-FR");
DateTime _date = DateTime.Parse("25/12/2004",_Format);

private static DateTimeFormatInfo getDateTimeFormatInfo(string p_Culture){

DateTimeFormatInfo _dtf;
try
{
CultureInfo _ci = new CultureInfo(p_Culture);
_dtf = _ci.DateTimeFormat;
return _dtf ;

}
catch {
return null;
}
}


Thanx
 
Thanx

Bret said:
Franck said:
I've been testing and it's works the way you tell me, but I still don't
know how to do it without specifiying time... :-(


string [] dateFormats = { "dd/MM/yyyy" };
DateTime.ParseStringExact(dateString, dateFormats, ...);

 
Back
Top