Solved

Check if String has a Valid Time

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Video Player 2017 5 20
Simple Injector with Web Service 4 38
Hey!! 5 33
Need to learn more about SecurityProtocolType.Tls12 3 27
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

813 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

10 Experts available now in Live!

Get 1:1 Help Now