Solved

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

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

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

Suggested Solutions

Title # Comments Views Activity
add projects t working set in maven 2 37
reading excel file in .net 2 30
linq .concat equivalent with expression syntax 4 22
Asp.Net Session Question 2 33
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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
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 …

679 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