In a C# 2008 application, I am obtaining a data from a web service via an xml file. In some instance a field is not a must not be a valid datetime value for a sql server 2008 r2 database.
Thus can you tell me:
1. what check I can do to make certain the value is in a valid date time format?
2. Also can you tell me how to convert the value to a valid datetime format?
Loading
VulpesPosted Oct 18, 2012, 1:55 PM
To get these formats (as strings) in C# you can do:
DateTime dt = DateTime.Now; // or whatever
string date = dt.ToString("yyyy-MM-dd");
string dateTime = dt.ToString("yyyy-MM-ddTHH:mm:ss");
So, I'd suggest you use one of these.
EhteshamPosted Oct 19, 2012, 5:50 AM
DateTime date;
string dateformat = "dd/MM/yyyy";
if (DateTime.TryParseExact("29/01/2012", dateformat, null, System.Globalization.DateTimeStyles.None, out date))
{
//Valid date
}
else
{
//invalid
}