Solved

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

Posted on 2009-04-15
3
224 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Check my code out(2) 2 53
C# Error - Add Failed 12 78
Definitions and default visual studio colors 5 58
Turn on intranet settings 1 36
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

932 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now