Đức Bùi
Đức Bùi

Reputation: 527

Why this wrong?

Value of record.Time is "11/01/2012 11:38:01:296"

string[] capturetime = record.Time.Split(':');
string captime = capturetime[0] + ":" + 
                 capturetime[1] + ":" + 
                 capturetime[2] + "." + 
                 capturetime[3];
DateTime rightTime = Convert.ToDateTime(captime);

After the second line run the value of captime is "11/01/2012 11:38:01.296" Error appear at the last line : "Index and length must refer to a location within the string. Parameter name: lengthmscorlib"

I dont understand why. Because it run fine before :( Anyone can explain this ? tks so much

Solved, tks everybody so much :)

Code :

 string[] capturetime = newRecord.Time.Split(':');
                string captime = capturetime[0] + ":" + capturetime[1] + ":" + capturetime [2] + "." + capturetime[3];
                DateTime righttime;

                if(time[3].Length == 2)
                {
                    righttime = DateTime.ParseExact(captime, "MM/dd/yyyy hh:mm:ss.ff",
                                                     CultureInfo.InvariantCulture);
                } else
                {
                    righttime = DateTime.ParseExact(captime, "MM/dd/yyyy hh:mm:ss.fff",
                                                    CultureInfo.InvariantCulture);
                }

So any idea for optimizing those code ????

Update :

Is it me or my Visual Studio is Crazy??? The new code above got exception like the old code and when i changed back to the old code. It work fine like the old day ..wth is this :(

Upvotes: 0

Views: 113

Answers (4)

BuddhiP
BuddhiP

Reputation: 6451

I believe the best way to solve this is by using the CultureInfo/DateTimeFormat strings.

However, if that's not applicable for you for some strange reason, here is a crude way of doing this.

        string val = @"11/01/2012 11:38:01:296";
        string[] parts = val.Split(' ');
        var dt = DateTime.ParseExact(parts[0], "dd/MM/yyyy", new DateTimeFormatInfo());
        double[] times = parts[1].Trim().Split(':').Select(s => double.Parse(s)).ToArray();
        dt = dt.AddHours(times[0]);
        dt = dt.AddMinutes(times[1]);
        dt = dt.AddSeconds(times[2]);
        dt = dt.AddMilliseconds(times[3]);

        // OR -- dt.AddMilliseconds(times[0] * 60 * 60 * 1000 + times[1] * 60 * 1000 + times[2] * 1000 + times[3]);

Upvotes: 0

BuddhiP
BuddhiP

Reputation: 6451

just use DateTime.ParseExact(val, "MM/dd/yyyy HH:mm:ss:fff", new DateTimeFormatInfo()) without doing all the string splitting.

Upvotes: 0

Asif Mushtaq
Asif Mushtaq

Reputation: 13150

You can use

string dateString =  "11/01/2012 11:38:01:296";    
DateTime date = DateTime.ParseExact(dateString , "MM/dd/yyyy hh:mm:ss:fff",
                                                CultureInfo.InvariantCulture);

Upvotes: 2

tempidope
tempidope

Reputation: 873

If you look closely, the time you have given, doesn't have the last COLON. As per normal standards, It would be a dot. Check RecordTime value properly.

"11/01/2012 11:38:01:296" is wrong "11/01/2012 11:38:01.296" is correct

Hence making the indices of captureTime wrong. It would be, 0, 1 and 2. 3 would be out of bounds.

Upvotes: 0

Related Questions