Rama Tito
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
Error message pop up for the following string: -
string MidNight = "23:59:59";
DateTime Mid_Night = DateTime.Parse(MidNight);
prod.png
That's valid where is the date portion?
It parses for me without issue:
-saige-
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();
}
}
}
Which produces the following output -That 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();
}
}
}
Which would produce the same output above.-saige-
ASKER
Hi,
DateTime Mid_Night = DateTime.Today.AddHours(23 ).AddMinut es(59).Add Seconds(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
DateTime Mid_Night = DateTime.Today.AddHours(23
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Also Works for me
DateTime Mid_Night = DateTime.Today.AddHours(23 ).AddMinut es(59).Add Seconds(59 );
radTextBox1.Text = Mid_Night.ToString();
DateTime Mid_Night = DateTime.Today.AddHours(23
radTextBox1.Text = Mid_Night.ToString();
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";
}
}
}
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.
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.
ASKER
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.
Open in new window
That said, Midnight is 00:00:00, so you will be 1 second off.