Solved

Converting string "2013-05-20 11:00:00.000000" to DateTime object

Posted on 2013-05-20
2
399 Views
Last Modified: 2013-05-20
I'm trying to parse a string of a specific format to a DateTime object, like this:

String pattern = "yyyy-MM-dd HH:mm:ss.ffffff";
DateTime SCHEDULED_START_date;
try {
   SCHEDULED_START_date = DateTime.ParseExact((String)queryResultTable["SCHEDULED_START"], pattern, CultureInfo.InvariantCulture);
} catch(Exception e) {
   throw new Exception("Could not parse datetime from TSM to DateTime object: " + e.ToString());
}

Open in new window


The string looks for instance like this:
2013-05-20 11:00:00.000000

I am not getting this done successfully.
0
Comment
Question by:itnifl
[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
2 Comments
 
LVL 42

Accepted Solution

by:
sedgwick earned 450 total points
ID: 39182049
String pattern = "yyyy-MM-dd HH:mm:ss.ffffff";
            DateTime SCHEDULED_START_date;
            try
            {
                SCHEDULED_START_date = DateTime.ParseExact("2013-05-20 11:00:00.000000", pattern, CultureInfo.CurrentCulture);
            }
            catch (Exception e)
            {
                throw new Exception("Could not parse datetime from TSM to DateTime object: " + e.ToString());
            }

Open in new window

this code works just fine, no exception is thrown.
make sure queryResultTable["SCHEDULED_START"] is not null or empty, and also u can use DateTime.TryParseExact() so no exception handling is needed.
0
 
LVL 42

Assisted Solution

by:sedgwick
sedgwick earned 450 total points
ID: 39182102
            String pattern = "yyyy-MM-dd HH:mm:ss.ffffff";
            DateTime SCHEDULED_START_date;
            bool parseOk = queryResultTable["SCHEDULED_START"] != null && DateTime.TryParseExact(queryResultTable["SCHEDULED_START"].ToString(), pattern, CultureInfo.CurrentCulture, DateTimeStyles.None, out SCHEDULED_START_date);
            if (!parseOk)
            {
                throw new Exception("Could not parse datetime from TSM to DateTime object: " + e.ToString());
            }

Open in new window

0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

623 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