Beginner
Beginner

Reputation: 29543

How to remove all characters after 3rd colon?

I have a date time span but currently a user can type in 12:00:00:00 and I get this error:

SqlDbType.Time overflow. Value '12:00:00:00' is out of range. Must be between 00:00:00.0000000 and 23:59:59.9999999.

So to stop them doing this I thought it would be easier if I strip all characters after the third colon. How do I do this?

Upvotes: 2

Views: 808

Answers (3)

Ian G
Ian G

Reputation: 30234

You want 12:00:00.00 not 12.00:00:00

Note fullstop in yours.

Try

        string badtimes = "12:00:00:00";
        string goodtimes = badtimes.Substring(0, badtimes.LastIndexOf(":")); // 12:00:00

Also check out SqlDbType.Time and TimeSpan incompatibility

Upvotes: 3

Nikola Davidovic
Nikola Davidovic

Reputation: 8656

You can try this

String time = "12:00:00:00";
        string[] parts = time.Split(new char [] {':'}, StringSplitOptions.RemoveEmptyEntries);
        string correctFormating = String.Format("{0}:{1}:{2}.{3}",parts);

Upvotes: 0

Yannick Blondeau
Yannick Blondeau

Reputation: 9621

The dot in your string is not placed correctly: 12.00:00:00 must be 12:00:00.00

Upvotes: 1

Related Questions