System.FormatException: 'String was not recognized as a valid DateTime.'

Rama Tito
Rama Tito used Ask the Experts™
on
System.FormatException: 'String was not recognized as a valid DateTime.'

Error message pop up for the following string: -
string MidNight = "23:59:59";

DateTime Mid_Night = DateTime.Parse(MidNight);
prod.png
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2015
Distinguished Expert 2018

Commented:
You could use:

DateTime Mid_Night = DateTime.Today.AddHours(23).AddMinutes(59).AddSeconds(59);

Open in new window

That said, Midnight is 00:00:00, so you will be 1 second off.
GMGeniusSoftware Development Manager/Developer

Commented:
That's valid  where is the date portion?

Commented:
It parses for me without issue:
using System;

namespace EE_Q29116282
{
	class Program
	{
		static void Main(string[] args)
		{
			var value = "23:59:59";
			var midnight = DateTime.Parse(value);
			Console.WriteLine(midnight);
			Console.ReadLine();
		}
	}
}

Open in new window

Which produces the following output -Capture.PNGThat being said, it could potentially be your culture settings.  You could try forcing a culture setting; e.g. -
using System;
using System.Globalization;

namespace EE_Q29116282
{
	class Program
	{
		static void Main(string[] args)
		{
			var value = "23:59:59";
			var midnight = DateTime.Parse(value, new CultureInfo("en-US"));
			Console.WriteLine(midnight);
			Console.ReadLine();
		}
	}
}

Open in new window

Which would produce the same output above.

-saige-
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

Rama TitoProgrammer

Author

Commented:
Hi,

DateTime Mid_Night = DateTime.Today.AddHours(23).AddMinutes(59).AddSeconds(59);   // i do get the same error

I am doing comparison between (DateTime.Now < Mid_Night), is that need date in Mid_Night, even though when i try above code still induce error as per attach.
datetime1.png
Commented:
I honestly do not think your error is happening on that exact line.  A DateTime.Today.AddHours().etc.etc is not parsing a string, it is a DateTime Object that is being assigned to another DateTime Object.

You have so many DateTime.Parse calls after this, that I truly believe that your fault is coming from one of those.  Your best option will be to debug and step through to identify which call is failing.

-saige-
Top Expert 2016

Commented:
Also Works for me
 DateTime Mid_Night = DateTime.Today.AddHours(23).AddMinutes(59).AddSeconds(59);
            radTextBox1.Text = Mid_Night.ToString();

2018-09-03_0-16-05.png
   public partial class RadForm1 : Telerik.WinControls.UI.RadForm
    {
        public RadForm1()
        {
            InitializeComponent();
        }

        private void radButton1_Click(object sender, EventArgs e)
        {
            DateTime Mid_Night = DateTime.Today.AddHours(23).AddMinutes(59).AddSeconds(59);
            radTextBox1.Text = Mid_Night.ToString();
            if (DateTime.Now < Mid_Night)
            {
                radTextBox2.Text="Time is Less Than Midnight";
            }
            else {
                radTextBox2.Text = "Time is Greater Than Midnight";
            }
        }
    }

Open in new window

2018-09-03_0-23-35.png
Most Valuable Expert 2015
Distinguished Expert 2018

Commented:
Saige is right. That code line cannot fail. Your error is raised form elsewhere.
Chinmay PatelChief Technology Ninja
Distinguished Expert 2018

Commented:
Hi Rama,

Your error is being generated at the line: 756, Please set up a debug point there and post the values (you can check the values,by selecting the variable and pressing CTRL + ALT + Q) of the following:

1. Moring_end_hours[3]
2. Moring_start_hours[3]

I believe, either both or one of them is having an invalid value.

Happy bug hunting,
Chinmay.
Rama TitoProgrammer

Author

Commented:
Hi Experts, Thank you for the feedback. Actually the error begin in different line. I did debug line by line and manage to resolve. Do appreciate most for given support.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial