Solved

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

Posted on 2013-05-20
2
398 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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
Using sample Autorize.net c# simple example 1 57
Adjust the codes 3 64
Best laptop programming computer for Windows development 14 87
JVM error from eclipse 1 27
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
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 …

752 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