I am trying a more efficient way to write this datetime function shown here, so that it can still give a correct date whether the datetime is in 24 hour notation or not. Currently, it works but throws errors when the notation changes.
using System;
using System.Web;
using System.Globalization;
class Program
{
private static string req = "01/26/2023 5:32 PM";
private static int hourNotation;
private static DateTime? expectedDate;
static void Main(string[] args)
{
Console.WriteLine("Hello, world!");
hourNotation = Convert.ToInt32(req[11].ToString());
// Task task = Task.Delay(1000);
if (hourNotation >= 1)
{
expectedDate = DateTime.ParseExact(req.Substring(0, req.Length - 3), "MM/dd/yyyy HH:mm",
new CultureInfo("en-US"),
DateTimeStyles.None);
Console.WriteLine("greater than 10");
}
else
{
expectedDate = DateTime.ParseExact(req.Substring(0, req.Length - 3), "MM/dd/yyyy h:mm",
new CultureInfo("en-US"),
DateTimeStyles.None);
Console.WriteLine("Less than 10");
}
}
}