473,386 Members | 2,114 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Setting System DateTime Now to be valid with XML

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);
}
}
Nov 16 '05 #1
3 2774
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.)

Nov 16 '05 #2
Answering my own question: use:
string testA = XmlConvert.ToString(DateTime.Now, "yyyy-MM-ddTHH:mm:sszzzzzz");
Nov 16 '05 #3
Yes, this is the right solution. Thank you for the share.

Luke
Microsoft Online Support

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

Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Jon Davis | last post by:
I have put my users through so much crap with this bug it is an absolute shame. I have a product that reads/writes RSS 2.0 documents, among other things. The RSS 2.0 spec mandates an en-US style...
0
by: Darren Clark | last post by:
I am trying to set the default value (not mistaken for NULL value) for a datetime element of a typed dataset. <xs:element name="create_date" type="xs:dateTime"...
8
by: tshad | last post by:
I have a string that I read from my database: 1|8|5620|541 These are all values in my ListBox. I want to select each of these items (4 of them - but could be many more). At the moment I am...
0
by: RSH | last post by:
I am having a bit of trouble filling a datatable with a query. I'm getting the error message: System.FormatException: String was not recognized as a valid DateTime. at...
10
by: Kiran | last post by:
Hi, I am trying to convert a string(Thu Jul 21 00:00:00 UTC+0100 200) to date. I get the following error Cast from string "Thu Jul 21 00:00:00 UTC+0100 200" to type 'Date' is not...
2
by: chrisshearier | last post by:
The below statement is causing System.InvalidCastException: Specified cast is not valid. Can anyone see what is going wrong with this and help me out. string statement =...
5
by: Rob Dob | last post by:
I am trying to set the NullValue within the Column properties of my Dataset in VS2005. The DataType is a System.DateTime. and when I try and change it from "(Throw Exception)" I get the following...
1
by: laredotornado | last post by:
Hi, I'm using PHP 4.4.4 on Apache 2 on Fedora Core 5. PHP was installed using Apache's apxs and the php library was installed to /usr/local/php. However, when I set my "error_reporting"...
0
by: Jedediah Marcus | last post by:
I would like to parse a hebrew date into a System::DateTime variable: System::Globalization::CultureInfo ^CulInfo = gcnew System::Globalization::CultureInfo("he-IL");...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.