Solved

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

Posted on 2013-05-20
2
387 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
  • 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
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…
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 …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

744 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

11 Experts available now in Live!

Get 1:1 Help Now