Solved

Try parsing a "s" not ss or mm or HH datetime format

Posted on 2009-04-15
3
225 Views
Last Modified: 2012-05-06
Hello,

my user can enter 10:10:10 or 10:10 or 10 then its a valid HH:mm:ss format. But when the user enters 1 instead of 01 (ss) seconds OR 1:10 instead of 01:10 (mm:ss) I want that it is parsed correctly to get a boolean of true changing the formats array to:

formats = new string[] {"HH:mm:ss"} , "mm:ss" , "ss","s"};

doesnt help, the "s" is not considered somehow...

Someone knows a solution?


string[] formats = new string[] { "HH:mm:ss", "mm:ss", "ss" }; 
 
public bool IsUserDateValid(String tempDate)
    { 
        bool isValid = false;
        string value = tempDate;
        DateTime time = new DateTime();
 
        if (DateTime.TryParseExact(value, formats, null, DateTimeStyles.None, out time))
            isValid = true;        
        else            
            isValid = false;
 
            return isValid;
    }

Open in new window

0
Comment
Question by:MSFanboy
  • 2
3 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 24147516
What about padding the input string yourself, before you test:
string[] formats = new string[] { "HH:mm:ss", "mm:ss", "ss" }; 
 
public bool IsUserDateValid(String tempDate)
    { 
        bool isValid = false;
        string[] parts = tempDate.Split(new char[] { ':' });
        string value;
        DateTime time = new DateTime();
 
        for (int x = 0; x < parts.Length(); x++)
        {
            parts[x] = parts[x].PadLeft(2, '0');
        }
 
        value = string.Join(":", parts);
        if (DateTime.TryParseExact(value, formats, null, DateTimeStyles.None, out time))
            isValid = true;        
        else            
            isValid = false;
 
            return isValid;
    }

Open in new window

0
 

Author Closing Comment

by:MSFanboy
ID: 31570400
ROCKS! did not know of PadLeft...
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 24147921
Glad 2 help :)
0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Connection String 16 61
In SQL Server 2016, can we configure MASKing rules that applies for application level users ? 12 58
Video Player 2017 5 25
Hey!! 5 33
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question