Solved

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

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Where did System.Data.Objects go? 2 41
C# LINQ ForEach() question 6 55
Sort GridView by ID Descending 1 17
C# MVC Insert Multiple Row into DB 2 32
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

828 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