Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2013-05-20
2
Medium Priority
?
400 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 1800 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 1800 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

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

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

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
What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
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…

660 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