Home > Mobile >  How to convert a string to datetime in ASP.NET Core using C#
How to convert a string to datetime in ASP.NET Core using C#

Time:01-30

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");
        }
    }
}

CodePudding user response:

You can greatly simplify this by using one the ParseExact() overloads that accepts an array of formats.

Just keep in mind: they mean it when they use "Exact" in the ParseExact() name. If your formats are not defined perfectly you'll get exceptions at run time. In this case, the format string for the 12 hour format is not right, because it's missing the time of day specifier. So you need this:

MM/dd/yyyy h:mm tt

Additionally, if you're allowing users to enter date or time values by hand, you WILL eventually (usually sooner than later) have users enter ambiguous or unparseable data. In this case, we see it expects the U.S. format, so a common failure is a visitor from the UK will naturally cause something like 29/1/2023 to go in the field, because that's how they and everyone else from that country have always done it. Even worse, they'll use 3/2/2023 expecting to get Feb 3 instead of March 2.

Better to have UI support to help the user select a date or time value via a guided process or clock/calendar control of some kind, and immediately show unambiguous feedback (using the written month name) of how an input will be interpreted.

CodePudding user response:

What about:

using System.Globalization;
class Program
{

    private static string req = "01/26/2023 21:32";
    private static List<string> dates = new List<string> { "01/26/2023 21:32", "01/26/2023 1:32", "01/26/2023 1:32 AM", "01/26/2023 11:32 PM" };


    private static DateTime? expectedDate;

    static void Main(string[] args)
    {

        Console.WriteLine("Hello, world!");
        foreach (string date in dates)
        {
            var isTwelveHour = date.ToUpper().Contains("PM") | date.ToUpper().Contains("AM");

            expectedDate = isTwelveHour
                ? DateTime.ParseExact(date, "MM/dd/yyyy h:mm tt",
                                                  new CultureInfo("en-US"),
                                                  DateTimeStyles.None)
                : DateTime.ParseExact(date, "MM/dd/yyyy H:mm",
                                                     new CultureInfo("en-US"),
                                                     DateTimeStyles.None);

            var clock = isTwelveHour ? "12 Hour Clock" : "24 Hour Clock";

            Console.WriteLine($"{clock} : {expectedDate.ToString()}");
        }
    }
}
  • Related