How do I parse a date using minutes that contains the value 60?

I am getting an exception when the minute contains the value 60

var date = "30/10/14 08:60";
var result = DateTime.ParseExact(date, "dd/MM/yy HH:mm", CultureInfo.InvariantCulture, DateTimeStyles.None);

      

How to disassemble it correctly?

+3


source to share


5 answers


If you always know if the temporary part of your string date

is in the format HH: mm, you can do this to get the correct one DateTime date

:



    string dateString = "30/10/14 08:60";
    string[] dateParts = dateString.Split(' ');
    DateTime date = DateTime.ParseExact(dateParts[0],"dd/MM/yy",CultureInfo.InvariantCulture);
    string[] timeParts = dateParts[1].Split(':');
    date=date.AddMinutes(double.Parse(timeParts[0])*60+double.Parse(timeParts[1]));

      

+1


source


Either pass the correct value ( >=0 || <=59

) or use this:



var date = "30/10/14 08:60";
DateTime dateResult;
bool canParse = DateTime.TryParseExact(date, "dd/MM/yy HH:mm", CultureInfo.InvariantCulture, DateTimeStyles.None, out dateResult);
if (!canParse)
{
    string datePart = date.Split().First();
    DateTime dtOnly;
    if (DateTime.TryParseExact(datePart, "dd/MM/yy", CultureInfo.InvariantCulture, DateTimeStyles.None, out dtOnly))
    {
        string timePart = date.Split().Last();
        string hourPart = timePart.Split(':')[0];
        string minutePart = timePart.Split(':').Last();
        int hour, minute;
        if (int.TryParse(hourPart, out hour) && int.TryParse(minutePart, out minute))
        {
            TimeSpan timeOfDay = TimeSpan.FromHours(hour) + TimeSpan.FromMinutes(minute);
            dateResult = dtOnly + timeOfDay;  // 10/30/2014 09:00:00
        }
    }
}

      

+3


source


First of all, the data is invalid and therefore an exception is thrown.

So basically there are 2 permissions:

  • If the data is obtained from a third party, my suggestion is that after consulting your bosses or company lawyers, you / your company ask the third party to provide reliable data, since you have no legal obligation to correct / allow inappropriate data for the third party. IMO, you shouldn't.

  • If the data is taken from your internal systems, you / the company must correct errors that can lead to 60. If for some reason the errors cannot be fixed soon, you can write a parser, for example, using a regular expression to parse the data and admit 60.

So 2nd resolution with regex is a direct answer to your question. However, remember that "10/30/14 08:60" is invalid and must be corrected sooner or later in the data source.

By the way, here's a link with some regular expressions you can try.

+3


source


For an international convention 60 minutes per hour. The sixtieth minute is 59, in fact, if you count from 0 to 59, you find they have 60 numbers. The date you write 8:60 does not exist, the value is 9:00.

Try it (obviously only date1 throws an exception):

var date = "30/10/14  8:59";
var date1 = "30/10/14 9:00";


var result = DateTime.ParseExact(date, "dd/MM/yy HH:mm", CultureInfo.InvariantCulture, DateTimeStyles.None);
var result1 = DateTime.ParseExact(date1, "dd/MM/yy HH:mm", CultureInfo.InvariantCulture, DateTimeStyles.None);

      

Also, you can use this to manipulate data and receive true / false message

var date = "30/10/14 08:60";
DateTime outData;
Boolean flagCorrectData = DateTime.TryParseExact(date, "dd/MM/yy HH:mm", CultureInfo.InvariantCulture, DateTimeStyles.None, out outData);
     if (flagCorrectData)
     {
        MessageBox.Show("Date correct");
     }
     else
     {
        MessageBox.Show("Date error");
     }

      

+2


source


If you're only interested in the special case: 60, you can explicitly use 60 in ParseExact

:

string date = "30/10/14 08:60";
DateTime result;

if(DateTime.TryParseExact(date, "dd/MM/yy HH:mm", 
    CultureInfo.InvariantCulture, DateTimeStyles.None,out result)) 
{
    return result;
}    

//Handle weird :60
if(DateTime.TryParseExact(date, "dd/MM/yy HH:60",
        CultureInfo.InvariantCulture, DateTimeStyles.None,out result))
{
    return result.AddMinutes(60);
}

throw new ArgumentException("date");

      

+1


source







All Articles