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

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
Rama TitoProgrammerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Gustav BrockCIOCommented:
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.
0
GMGeniusSoftware Development Manager/DeveloperCommented:
That's valid  where is the date portion?
0
it_saigeDeveloperCommented:
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-
0
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

Rama TitoProgrammerAuthor 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
0
it_saigeDeveloperCommented:
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-
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
David Johnson, CD, MVPOwnerCommented:
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
0
Gustav BrockCIOCommented:
Saige is right. That code line cannot fail. Your error is raised form elsewhere.
0
Chinmay PatelChief Technical NinjaCommented:
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.
0
Rama TitoProgrammerAuthor 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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.