Solved

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

Posted on 2009-04-15
3
227 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

739 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