Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 549
  • Last Modified:

String was not recognized as a valid DateTime

I am reading data from a table that has a date in it.  It is formatted as follows: 20060608135000

My code is as follows:
myLabData.ObservationDateTime = Convert.ToDateTime(labobxrecord.labobxdata.ObservationDateTime);

What command should I use to covert it to the proper date format?
0
kwh3856
Asked:
kwh3856
  • 4
  • 3
  • 2
1 Solution
 
käµfm³d 👽Commented:
What does "20060608135000" represent? Is it ticks since some date? Is it some other specialized format?
0
 
kwh3856Author Commented:
Year: 2006
Month:06
Day:08
Time:13:30
Seconds:00

It is a date and time that is documented by a physical device creating the lab result.
0
 
saraganiCommented:
Ok, so:
            string date = "20060608135000";
            int year = Convert.ToInt32(date.Substring(0, 4));
            int month = Convert.ToInt32(date.Substring(4, 2));
            int day = Convert.ToInt32(date.Substring(6, 2));
            int hour = Convert.ToInt32(date.Substring(8, 2));
            int minutes = Convert.ToInt32(date.Substring(10, 2));
            int seconds = Convert.ToInt32(date.Substring(12, 2));

            DateTime dateTime = new DateTime(year, month, day, hour, minutes, seconds);
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
saraganiCommented:
Btw, the code will give that the minutes are 50 and not 30.

Are you sure it is 13:30 and not 13:50? I'm seeing 135000 in the string you gave us.

0
 
kwh3856Author Commented:
Thanks...you are correct.  My typo:)

0
 
kwh3856Author Commented:
Thanks
0
 
käµfm³d 👽Commented:
I would have preferred:
myLabData.ObservationDateTime = DateTime.ParseExact(labobxrecord.labobxdata.ObservationDateTime, "yyyyMMddhhmmss", System.Globalization.CultureInfo.CurrentCulture);

Open in new window

0
 
kwh3856Author Commented:
Kaufmed,
I like that one to.  I am sorry I could not award any points.  I had already accepted the previous answer.  Thank you though for posting.  I know from the past, I always learn something new when you post up answers for me.

Thanks
Kenny
0
 
käµfm³d 👽Commented:
It's cool. Glad to help  = )
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now