none
Convert time to HH:mm format RRS feed

  • Question

  • Hi Team,

    App.Config file key: 

        <add key="ProcessTimeInterval" value="14:30"/>  <!--Process Time Interval is in HH:mm in 24 hour time format-->



    Class file code:

      var ProcessDateTime = DateTime.ParseExact(ConfigurationValues.ProcessTimeInterval, "HH:mm", null, System.Globalization.DateTimeStyles.None);

    I have a config key with excepted time format is "HH:mm".  I am facing issues at  var ProcessDateTime  when the config key value is 

    Example:
       <add key="ProcessTimeInterval" value="7:30"/>
       <add key="ProcessTimeInterval" value="7:3"/>
       <add key="ProcessTimeInterval" value="7"/>

    Need inputs to handle the below scenarios because the below time format is not in "HH:mm".

    1) when the config key value is "7:30" format  THEN convert the "7:30"  to  "07:30" in the class file code to make it to "HH:mm" format.

    2) when the config key value is "7:3" format  THEN convert the "7:3" to  "07:03" in the class file code to make it to "HH:mm" format.

    3) when the config key value is "7" format  THEN  convert the "7" to  "07:00" in the class file code to make it to "HH:mm" format.


    Vamshi Janagama

    Thursday, November 21, 2019 6:37 PM

Answers

    • Marked as answer by Vamshi K J Tuesday, December 3, 2019 3:10 PM
    Thursday, November 21, 2019 6:44 PM
  • Hello,

    The following works againsts a string so I bypassed mocking up a config file.

    using System;
    
    namespace YourNamespaceGoesHere
    {
        public static class TimeSpanExtensions
        {
            public static string AdjustHourMinutes(this string sender)
            {
    
                if (!sender.Contains(":"))
                {
                    return sender.Length == 1 ? $"0{sender}:00" : $"{sender}:00";
                }
                else
                {
                    var parts = sender.Split(':');
                    return parts[1].Length == 2 ? $"{Convert.ToInt32(parts[0]):D2}:{parts[1]}" : 
                        $"{Convert.ToInt32(parts[0]):D2}:0{parts[1]}";
                }
            }
        }
    }
    

    Test

    var ProcessDateTime1 = DateTime.ParseExact("7".AdjustHourMinutes(), "HH:mm", null, 
        System.Globalization.DateTimeStyles.None);
    
    Console.WriteLine(ProcessDateTime1.ToString("t"));
    
    var ProcessDateTime2 = DateTime.ParseExact("7:30".AdjustHourMinutes(), "HH:mm", null, 
        System.Globalization.DateTimeStyles.None);
    
    Console.WriteLine(ProcessDateTime2.ToString("t"));
    
    var ProcessDateTime3 = DateTime.ParseExact("7:3".AdjustHourMinutes(), "HH:mm", null, 
        System.Globalization.DateTimeStyles.None);
    
    Console.WriteLine(ProcessDateTime3.ToString("t"));
    
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Vamshi K J Tuesday, December 3, 2019 3:10 PM
    Thursday, November 21, 2019 7:53 PM
    Moderator

All replies

    • Marked as answer by Vamshi K J Tuesday, December 3, 2019 3:10 PM
    Thursday, November 21, 2019 6:44 PM
  • Hello,

    The following works againsts a string so I bypassed mocking up a config file.

    using System;
    
    namespace YourNamespaceGoesHere
    {
        public static class TimeSpanExtensions
        {
            public static string AdjustHourMinutes(this string sender)
            {
    
                if (!sender.Contains(":"))
                {
                    return sender.Length == 1 ? $"0{sender}:00" : $"{sender}:00";
                }
                else
                {
                    var parts = sender.Split(':');
                    return parts[1].Length == 2 ? $"{Convert.ToInt32(parts[0]):D2}:{parts[1]}" : 
                        $"{Convert.ToInt32(parts[0]):D2}:0{parts[1]}";
                }
            }
        }
    }
    

    Test

    var ProcessDateTime1 = DateTime.ParseExact("7".AdjustHourMinutes(), "HH:mm", null, 
        System.Globalization.DateTimeStyles.None);
    
    Console.WriteLine(ProcessDateTime1.ToString("t"));
    
    var ProcessDateTime2 = DateTime.ParseExact("7:30".AdjustHourMinutes(), "HH:mm", null, 
        System.Globalization.DateTimeStyles.None);
    
    Console.WriteLine(ProcessDateTime2.ToString("t"));
    
    var ProcessDateTime3 = DateTime.ParseExact("7:3".AdjustHourMinutes(), "HH:mm", null, 
        System.Globalization.DateTimeStyles.None);
    
    Console.WriteLine(ProcessDateTime3.ToString("t"));
    
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Vamshi K J Tuesday, December 3, 2019 3:10 PM
    Thursday, November 21, 2019 7:53 PM
    Moderator
  • Hi Kareninstructor

    When i am executing MSBuild I am facing issue "error CS1056: Unexpected character '$' " in the below method.  The issue can be solved  if i use string.Format instead of  $.    please provide inputs.  


     public static string AdjustHourMinutes(this string sender)
            {
    
                if (!sender.Contains(":"))
                {
                    return sender.Length == 1 ? $"0{sender}:00" : $"{sender}:00";
                }
                else
                {
                    var parts = sender.Split(':');
                    return parts[1].Length == 2 ? $"{Convert.ToInt32(parts[0]):D2}:{parts[1]}" : 
                        $"{Convert.ToInt32(parts[0]):D2}:0{parts[1]}";
                }
            }

    Thank you.


    Vamshi Janagama

    Monday, December 16, 2019 10:19 PM