Solved

Check if String has a Valid Time

Posted on 2011-03-03
3
465 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 74

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 74

Expert Comment

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

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

708 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

16 Experts available now in Live!

Get 1:1 Help Now