Setting System DateTime Now to be valid with XML

G

Guest

I save date time into my XML files like this:
MesObj.theTime=System.DateTime.Now;
"5/4/2004 8:50:30 AM"
BUT that string is NOT recognizable as a valid
dateTime when the data is read back from file ( i.e. schema strong data type dateTime
It seems to follow this test protocal : DateTime transactDate = XmlConvert.ToDateTime(timeTest);

I found the following table at MSDN and put it through this code to validate it:
[ but only 2 formats were valid ....
See Console output & snippet below:

Since I write the MesObj.theTime to my files for many transactions, I need to change it so
that my tools can reread the transaction datetime as a valid dateTime for sorting.

What should I do to get SUCCESS... ??? - Is there a one line piece of code to get that format ? Or do I have to build up the date string to be of form 2001-04-10T15:51:24 ?? THANKS !!!



/****************
d en-US 4/10/2001
d en-NZ 10/04/2001
d de-DE 10.04.2001
D en-US Tuesday, April 10, 2001
T en-US 3:51:24 PM
T es-ES 15:51:24
f en-US Tuesday, April 10, 2001 3:51 PM
f fr-FR mardi 10 avril 2001 15:51
r en-US Tue, 10 Apr 2001 15:51:24 GMT
r zh-SG Tue, 10 Apr 2001 15:51:24 GMT
s en-US 2001-04-10T15:51:24
s pt-BR 2001-04-10T15:51:24
u en-US 2001-04-10 15:51:24Z
u sv-FI 2001-04-10 15:51:24Z
m en-US April 10
m ms-MY 10 April
y en-US April, 2001
y af-ZA April 2001
L en-UZ Unrecognized format specifier; a format exception is thrown
****************************/


Try again 4/10/2001
Try again 10/04/2001
Try again 10.04.2001
Try again Tuesday, April 10, 2001
Try again 3:51:24 PM
Success 15:51:24
Try again Tuesday, April 10, 2001 3:51 PM
Try again Tue, 10 Apr 2001 15:51:24 GMT
Try again Tuesday, April 10, 2001 3:51 PM
Success 2001-04-10T15:51:24
Try again 2001-04-10 15:51:24Z
Try again 10 April
Try again April, 2001
Try again April 2001

// snippet:

ArrayList dateStyle = new ArrayList();
dateStyle.Add("4/10/2001");
dateStyle.Add("10/04/2001");
dateStyle.Add("10.04.2001");
dateStyle.Add("Tuesday, April 10, 2001");
dateStyle.Add("3:51:24 PM");
dateStyle.Add("15:51:24");
dateStyle.Add("Tuesday, April 10, 2001 3:51 PM");
dateStyle.Add("Tue, 10 Apr 2001 15:51:24 GMT");
dateStyle.Add("Tuesday, April 10, 2001 3:51 PM");
dateStyle.Add("2001-04-10T15:51:24");
dateStyle.Add("2001-04-10 15:51:24Z");
dateStyle.Add("10 April");
dateStyle.Add("April, 2001");
dateStyle.Add("April 2001");

foreach (string timeTest in dateStyle)
{
try
{
DateTime transactDate = XmlConvert.ToDateTime(timeTest);
Console.WriteLine("Success " + timeTest);
}
catch ( Exception e)
{
Console.WriteLine("Try again " + timeTest);
}
}
 
M

[MSFT]

Hello,

XmlConvert.ToDateTime method can only convert the string in format
"yyyy-MM-ddTHH:mm:sszzzzzz" and its subsets. If you want to get the
DateTime object from the string in the list, you may try DateTime.Parse
method. For example:

DateTime myDateTime = DateTime.Parse(timeTest);

Console.WriteLine("1) myDateTime = {0}", myDateTime);

Hope this help,

Luke
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
G

Guest

Answering my own question: use:
string testA = XmlConvert.ToString(DateTime.Now, "yyyy-MM-ddTHH:mm:sszzzzzz");
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top