Link to home
Start Free TrialLog in
Avatar of Rama Tito
Rama TitoFlag for Malaysia

asked on

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
Avatar of Gustav Brock
Gustav Brock
Flag of Denmark image

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.
That's valid  where is the date portion?
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 -User generated imageThat 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-
Avatar of Rama Tito

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of it_saige
it_saige
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Also Works for me
 DateTime Mid_Night = DateTime.Today.AddHours(23).AddMinutes(59).AddSeconds(59);
            radTextBox1.Text = Mid_Night.ToString();

User generated image
   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

User generated image
Saige is right. That code line cannot fail. Your error is raised form elsewhere.
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.
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.