2

I need to extract datetimes from xml in both long (yyyy-MM-dd HH:mm:ss) and short (yyyy-MM-dd) format from an xml doc. Date format can vary by locality e.g. MM/dd/yyyy vs yyyy-mm-dd.

The app is run in each locality so will know what the local localisation is.

Question: How do I 1. extract dates from the xml node inner text using whatever is the local date format 2. extract the dates using long or short format given that I may not know which has been supplied in the xml node

Dave Tapson
  • 810
  • 1
  • 9
  • 22
  • What do you mean? That the XML data contains text fields with `MM/dd/yyyy` values? The `xs:date` and `xs:dateTime` types use the ISO8601 format so you should probably fix the code that generates the XML file. .NET's DateTime objects have no format so you won't have to deal with localization until you render the value as a string eg for display – Panagiotis Kanavos Sep 20 '16 at 09:04
  • To put it another way, locale-specific date strings in XML is a bug that has to be hand-coded, as .NET will serialize dates correctly – Panagiotis Kanavos Sep 20 '16 at 09:06
  • I don't generate the xml - I get it as is. I want to extract the dates, load them into datetime pickers to allow the user to update dates with new dates and then save them in the same format they were in originally back into the xml. – Dave Tapson Sep 20 '16 at 09:15
  • That didn't answer the comment. Is the value really in the `MM/dd/yyyy` form? If the client and source have the same locale, a simple `DateTime.Parse` is enough. You can also pass a specific CultureInfo parameter to all the Parse functions – Panagiotis Kanavos Sep 20 '16 at 09:25
  • 2
    As far as a DatePicker is concerned, what it displays depends on the user's locale too, so it shouldn't need any modification to display DateTime values. Even if it does, you don't have to modify the *value*, you can modify the control's format property or even change the application's UICulture – Panagiotis Kanavos Sep 20 '16 at 09:29

2 Answers2

3

You might be interested in the methods DateTime.TryParseExact or DateTime.ParseExact. Then create a set of allowed format strings and loop throuh this set to test parse the date from the most specific formats first:

var formats = new List<string>();
formats.Add("yyyy-MM-dd HH:mm:ss");
formats.Add("yyyy-MM-dd");
formats.Add("MM/dd/yyyy");
formats.Add("yyyy-mm-dd");

DateTime parsedDate;
foreach(var format in formats)
{
    if (DateTime.TryParseDate(str, format, CultureInfo.InvariantCulture,   DateTimeStyles.None, out parsedDate))
    {   
        break;
    }
}

Edited: The correct format is ISO 8601 as described here:

What is the correct format to use for Date/Time in an XML file

If not using the standard format, there could be a conflict between similar formats, for example between dd/MM/yyyy and MM/dd/yyyy which are both valid in some regions.

Community
  • 1
  • 1
Martin Staufcik
  • 8,295
  • 4
  • 44
  • 63
  • Thanks, this looks interesting... the only issue with this would be that I'd need to include the main formats used around the world too... – Dave Tapson Sep 20 '16 at 09:15
  • 1
    *If and only if* the data and client use the same locale, a simple `DateTime.Parse` is enough as it will use the client's locale. There is no need for `ParseExact`. All parse functions also accept a CultureInfo parameter which is a safer way of specifying the locale than actually hand-coding the format – Panagiotis Kanavos Sep 20 '16 at 09:23
  • The XML does not use the ISO format. Hence my problem. I was hoping to use be able to establish the local format which would then indicate if the date was in dd/MM/yyyy or MM/dd/yyyy... Edit: data and client will be on same machine so same locale. – Dave Tapson Sep 20 '16 at 09:23
  • 1
    @DaveTapson .NET already uses the local format. You don't need to specify it. `DateTime.Parse(someString)` or `DateTime.TryParse` will use the format specified in the user's regional settings. Did you try this? Did you encounter a problem? – Panagiotis Kanavos Sep 20 '16 at 09:26
1

As suggested by Panagiotis Kanavos, the dates in xml should be of the ISO8601 format. Here is an example of such :

string xmlInput =  @"
<root>
<element>
<timestamp time='2016-09-15T13:45:30'>
</timestamp>
</element>
<element>
<timestamp time='2016-10-16T13:45:30'>
</timestamp>
</element>
</root>";

XDocument  xdoc = XDocument.Parse(xmlInput);
var listOfDates = xdoc.Descendants("root").Elements("element").Select(x => DateTime.Parse(x.Element("timestamp").Attribute("time").Value,CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind)).ToList<DateTime>();
Console.WriteLine(listOfDates[0]);
Richa Garg
  • 1,888
  • 12
  • 23