Solved

Check if String has a Valid Time

Posted on 2011-03-03
3
474 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
[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: 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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

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!
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

733 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