Solved

Check if String has a Valid Time

Posted on 2011-03-03
3
467 Views
Last Modified: 2012-05-11
I have clients passing a string to my WCF Service for processing using an SQL SP. One of the parameters they pass is a String Value, which should be a DateTime in String Format. (Service does all TryParse etc..)

This must stay as it is, as other services use this which I can't change.

What I need to do is somehow Check the String server side, to see if the String has a valid time, as well as a Date, before I pass it to the SP. If it doesn't, I need to add a time of '23:59:59.997', if it has, then I leave it alone, because one the the clients has specified a Time in their request. (i.e. some clients pass '2011/2/21' other might pass '2011/2/21 14:14:00'.

The point is that sometimes a client passes a String with Date AND Time, other times the client may pass a Datetime in ShortDate format.

I can't change the client, so need to do the checking server side.

Thanks.
0
Comment
Question by:wint100
  • 2
3 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 35030943
Would this be adequate?
string incoming_string = "2011/2/21 14:14:00";
DateTime parsedDate;

if (DateTime.TryParse(incoming_string, out parsedDate))
{
    // Date is valid

    string partial = incoming_string.Trim();

    partial = partial.Substring(partial.LastIndexOf("/") + 1);

    if (partial.Length <= 2)
    {
        // No time was entered
        parsedDate = parsedDate.AddHours(23).AddMinutes(59).AddSeconds(59).AddMilliseconds(997);
    }
}
else
{
    // Date invalid
}

Open in new window

0
 
LVL 1

Author Comment

by:wint100
ID: 35036371
That should do it, thanks.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35037026
NP. Glad to help  : )
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

911 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

22 Experts available now in Live!

Get 1:1 Help Now