1

I have to change a csv file with several dates in it. Every row starts with a date followed whith data. 11-nov-2015,data,data,data 10-nov-2015,data,data,data 9-nov-2015,data,data,data

With the following code I put the data in the right place (20141109 (yyyymmdd))

string[] values = lines1[i].Split(',');

if (values.Length >= 3)
{
    string[] parts = values[0].Split('-');
    if (parts.Length == 3)
    {
        values[0] =  String.Format("{0}-{1}-{2}", parts[2], parts[1], parts[0]);
        lines1[i] = String.Join(",", values);
    }
}

But two problems remain: 1) The month has to change from nov to 11

2) In the file I download the day for example 9-nov-2014 has to change to 09. for 8-nov-2014 to 08. So an extra 0.

How can this be solved in C#

lesscode
  • 6,221
  • 30
  • 58
Joske
  • 35
  • 2
  • 9
  • Splitting on ',' is generally a bad idea since content can contain ',' in .csv files. See here for a better approach: http://stackoverflow.com/questions/3268622/regex-to-split-line-csv-file – lesscode Nov 29 '15 at 18:40

2 Answers2

3

Instead of making your own datetime format parser, you should use the one already available for you. DateTime.TryParseExact is your tool to convert a string in a date when you know the exact format.
Converting back the date, in the string format that you like, is another task easily solved by the override of ToString() specific for a datetime

string[] values = lines1[i].Split(',');
if (values.Length >= 3)
{
    DateTime dt;
    if (DateTime.TryParseExact(values[0], "d-MMM-yyyy", 
        System.Globalization.CultureInfo.CurrentCulture, 
        System.Globalization.DateTimeStyles.None, out dt))
    {
        values[0] = dt.ToString("yyyyMMdd");
        lines1[i] = String.Join(",", values);
    }
}
Steve
  • 213,761
  • 22
  • 232
  • 286
  • The above solutions works for me fine. Only the dates are in US format so there is no conversion for example if the date is oktober. The European notation is okt but US oct. How can this be solved? – Joske Dec 08 '15 at 16:05
  • Well, it is always a difficult task to test these conditions when you are in a different locale. You could try to remove the CurrentCulture parameter and pass a null or you could try to experiment making an instance of the [CultureInfo with a specific locale](https://msdn.microsoft.com/en-us/en-en/goglobal/bb896001.aspx) matching you dates formats IE: _CultureInfo ci = new CultureInfo("de-DE")_ and check the result. I suggest to use a tool like LinqPAD to test your snippets of code looking for the correct combination of date string and CultureInfo. – Steve Dec 08 '15 at 16:53
1

I would parse the string into a date and then write it back using a custom date format. From this link we can write this code:

String pattern = "dd-MMM-yyyy";
DateTime dt;
if (DateTime.TryParseExact(values[0], pattern, CultureInfo.InvariantCulture, 
                           DateTimeStyles.None,
                           out dt)) {
    // dt is the parsed value
    String sdt = dt.ToString("yyyyMMdd"); // <<--this is the string you want
} else {
    // Invalid string, handle it as you see fit
}
Community
  • 1
  • 1
chrisl08
  • 1,658
  • 1
  • 15
  • 24