Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2009-04-15
3
Medium Priority
?
233 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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

885 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