none
Int Input and convert to Time Value (Input 1300 = 1pm)

    Question

  • I need in a console application to have user input 1300 or 1330 and have that be converted somehow to 13:00 and 13:30 instead of 1330 the pure number.

    So that I can then take 1330 and have the user input an end time like 1430 and increase it by 25 percent to come out with 1445 or 2:45

    Monday, June 04, 2012 10:16 PM

Answers

  • Hi,

    This is a very rustic method but I hope it help you

    string aaa = "2030";//user input
    
    string ccc = aaa[0].ToString() + aaa[1].ToString() + ":" + 
    aaa[2].ToString() + aaa[3].ToString();
    
    Label1.Text = ccc;//result: 20:30



    Jose Mendez.
    Blog
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Si un Post reponde a tu pregunta, por favor "Marcala como Respondida" y "Vota como útil".

    Monday, June 04, 2012 10:47 PM
  • For a scenario like this, a regular expression can do all of the heavy lifting for you, both validating the input and splitting it into its component parts.

        public static class MilTimeHelper
        {
            private static Regex MilTimeRegex = new Regex(@"^(?<hours>[0-1][0-9]|[2][0-3])(?<minutes>[0-5][0-9])$");
    
            // We'll use a pattern similar to the TryParse methods, returning a bool to indicate the 
            // success or failure of our parsing, and returning the result in an out parameter.
            public static bool MilTimeToDateTime(string input, out DateTime output)
            {
                Match match = MilTimeRegex.Match(input);
                if (!match.Success)
                {
                    output = DateTime.MinValue;
                    return false;
                }
                else
                {
                    // As a consequence of the regular-expression matching, we don't need to be too 
                    // careful about these int.Parse(string) calls.
                    int hours = int.Parse(match.Groups["hours"].Value);
                    int minutes = int.Parse(match.Groups["minutes"].Value);
    
                    // I'm using today's date to fill out the structure but you could use any 
                    // arbitrary "null" date that makes sense in your context.
                    DateTime now = DateTime.Now;
                    output = new DateTime(now.Year, now.Month, now.Day, hours, minutes, 0);
                    
                    return true;
                }
            }
        }
    }
    Then, once you've done any manipulation on the time that you need to do, just use the DateTime.ToString( string format ) method to get back a hhmm string.
     
    Tuesday, June 05, 2012 12:16 AM
  • Hi, 

    Check with, 

    private void button1_Click(object sender, EventArgs e)
    		{
    			int startTime = 1300;
    			int endTime = 1330;
    
    			int startTimeInHour = startTime / 100;
    
    			int startTimeInMin = (startTimeInHour * 60) + startTime % 100;
    
    			int endTimeInHour = endTime / 100;
    			int endTimeMin = Convert.ToInt32(endTime % 100 + (0.25 * 60)); // 13:45
    
    			int endTimeInMin = endTimeInHour * 60 + endTimeMin;
    
    			int differenceMin = endTimeInMin - startTimeInMin;
    
    			//conversion from minutes back to hour:minute format
    			int totalTripH = differenceMin / 60;
    			int totalTripM = differenceMin - (totalTripH * 60);
    
    			//output
    			var k = string.Format("Total Trip Time: {0}:{1}", totalTripH, totalTripM);
    		}

    Hope this helps you...


    If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".

    Tuesday, June 05, 2012 1:50 AM
  • Hi,

    here is something that may help you.

    string inputStartTime = "1300";
                    string inputEndTime = "1430";
                    //you may add the validation that first two digits are between 00 and 23 and 
                    //last bits are between 00 and 59
                    TimeSpan startTime = new TimeSpan(Convert.ToInt32(inputStartTime.Substring(0, 2)), Convert.ToInt32(inputStartTime.Substring(2)), 0);
                    Console.WriteLine(startTime.ToString());
                    TimeSpan endTime = new TimeSpan(Convert.ToInt32(inputEndTime.Substring(0, 2)), Convert.ToInt32(inputEndTime.Substring(2)), 0);
                    Console.WriteLine(endTime.ToString());
                    double totalIntervalAsMinutes = endTime.Subtract(startTime).TotalMinutes;
                    int minutesToAdd = Convert.ToInt32((totalIntervalAsMinutes * 25) / 100);
                    int hour = (minutesToAdd >= 60) ? (minutesToAdd / 60) : 0;
                    int minute = (hour > 0) ? (minutesToAdd - (hour * 60)) : minutesToAdd;
                    endTime = endTime.Add(new TimeSpan(hour, minute, 0));
                    Console.WriteLine(endTime.ToString()); 
                    // or simply add 15 minutes 
                    endTime = new TimeSpan(Convert.ToInt32(inputEndTime.Substring(0, 2)), Convert.ToInt32(inputEndTime.Substring(2)), 0);
                    endTime = endTime.Add(new TimeSpan(0, 15, 0));
                    Console.WriteLine(endTime.ToString());


    Mohammad Saidul Karim

    Tuesday, June 05, 2012 3:30 AM

All replies

  • Hi,

    This is a very rustic method but I hope it help you

    string aaa = "2030";//user input
    
    string ccc = aaa[0].ToString() + aaa[1].ToString() + ":" + 
    aaa[2].ToString() + aaa[3].ToString();
    
    Label1.Text = ccc;//result: 20:30



    Jose Mendez.
    Blog
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Si un Post reponde a tu pregunta, por favor "Marcala como Respondida" y "Vota como útil".

    Monday, June 04, 2012 10:47 PM
  • For a scenario like this, a regular expression can do all of the heavy lifting for you, both validating the input and splitting it into its component parts.

        public static class MilTimeHelper
        {
            private static Regex MilTimeRegex = new Regex(@"^(?<hours>[0-1][0-9]|[2][0-3])(?<minutes>[0-5][0-9])$");
    
            // We'll use a pattern similar to the TryParse methods, returning a bool to indicate the 
            // success or failure of our parsing, and returning the result in an out parameter.
            public static bool MilTimeToDateTime(string input, out DateTime output)
            {
                Match match = MilTimeRegex.Match(input);
                if (!match.Success)
                {
                    output = DateTime.MinValue;
                    return false;
                }
                else
                {
                    // As a consequence of the regular-expression matching, we don't need to be too 
                    // careful about these int.Parse(string) calls.
                    int hours = int.Parse(match.Groups["hours"].Value);
                    int minutes = int.Parse(match.Groups["minutes"].Value);
    
                    // I'm using today's date to fill out the structure but you could use any 
                    // arbitrary "null" date that makes sense in your context.
                    DateTime now = DateTime.Now;
                    output = new DateTime(now.Year, now.Month, now.Day, hours, minutes, 0);
                    
                    return true;
                }
            }
        }
    }
    Then, once you've done any manipulation on the time that you need to do, just use the DateTime.ToString( string format ) method to get back a hhmm string.
     
    Tuesday, June 05, 2012 12:16 AM
  • Hi, 

    Check with, 

    private void button1_Click(object sender, EventArgs e)
    		{
    			int startTime = 1300;
    			int endTime = 1330;
    
    			int startTimeInHour = startTime / 100;
    
    			int startTimeInMin = (startTimeInHour * 60) + startTime % 100;
    
    			int endTimeInHour = endTime / 100;
    			int endTimeMin = Convert.ToInt32(endTime % 100 + (0.25 * 60)); // 13:45
    
    			int endTimeInMin = endTimeInHour * 60 + endTimeMin;
    
    			int differenceMin = endTimeInMin - startTimeInMin;
    
    			//conversion from minutes back to hour:minute format
    			int totalTripH = differenceMin / 60;
    			int totalTripM = differenceMin - (totalTripH * 60);
    
    			//output
    			var k = string.Format("Total Trip Time: {0}:{1}", totalTripH, totalTripM);
    		}

    Hope this helps you...


    If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".

    Tuesday, June 05, 2012 1:50 AM
  • Hi,

    here is something that may help you.

    string inputStartTime = "1300";
                    string inputEndTime = "1430";
                    //you may add the validation that first two digits are between 00 and 23 and 
                    //last bits are between 00 and 59
                    TimeSpan startTime = new TimeSpan(Convert.ToInt32(inputStartTime.Substring(0, 2)), Convert.ToInt32(inputStartTime.Substring(2)), 0);
                    Console.WriteLine(startTime.ToString());
                    TimeSpan endTime = new TimeSpan(Convert.ToInt32(inputEndTime.Substring(0, 2)), Convert.ToInt32(inputEndTime.Substring(2)), 0);
                    Console.WriteLine(endTime.ToString());
                    double totalIntervalAsMinutes = endTime.Subtract(startTime).TotalMinutes;
                    int minutesToAdd = Convert.ToInt32((totalIntervalAsMinutes * 25) / 100);
                    int hour = (minutesToAdd >= 60) ? (minutesToAdd / 60) : 0;
                    int minute = (hour > 0) ? (minutesToAdd - (hour * 60)) : minutesToAdd;
                    endTime = endTime.Add(new TimeSpan(hour, minute, 0));
                    Console.WriteLine(endTime.ToString()); 
                    // or simply add 15 minutes 
                    endTime = new TimeSpan(Convert.ToInt32(inputEndTime.Substring(0, 2)), Convert.ToInt32(inputEndTime.Substring(2)), 0);
                    endTime = endTime.Add(new TimeSpan(0, 15, 0));
                    Console.WriteLine(endTime.ToString());


    Mohammad Saidul Karim

    Tuesday, June 05, 2012 3:30 AM