Zesa Rex
Zesa Rex

Reputation: 482

how to work with double in textbox?

I would like to write a code that reads a time the user wrote into textbox1, lets say 09:00 (hh:mm), save it into a variable, then takes this variable and adds 08:40 to it and Outputs then the outcome 17:40 into textbox2.

The Problem is, when working with double it is working well, but I need to use the hh:mm - Format, I know some languages have the Option to declare a variable as time but it seems like c# isnt doing this. so whats the solution? thats my current code

    void CmdWriteClick(object sender, EventArgs e)
    {
        string varstr = textbox_1.Text;
        double muss = 8.40;
        double vardb = Convert.ToDouble(varstr);
        double end = vardb + muss;
        textbox_2.Text = end.ToString();
    }

this would of course Output me double-numbers. so if varstr is 08.30 it would Output 16,7 when I Need to make it Output 17:10. Any help is appreciated

thank you guys

Upvotes: 1

Views: 623

Answers (5)

Icepickle
Icepickle

Reputation: 12796

If you really want to do it with double, you have to think about the conversion from minutes to whole numbers. 8.4 would be then 60 * .4 = 24 minutes.

For the rest, the text string you can parse using TimeSpan.Parse and you can write a custom function that would parse the double to a TimeSpan and then simply add the 2 together, like so:

using System;

namespace TimeParser {
    class Program {
        static TimeSpan GetTimeFromText( string text ) {
            if ( string.IsNullOrWhiteSpace( text ) ) {
                return TimeSpan.Zero;
            }
            return TimeSpan.Parse( text );
        }

        static TimeSpan GetTimeFromDouble( double value ) {
            if ( value <= 0 ) {
                return TimeSpan.Zero;
            }
            int hours = (int)Math.Floor( value );
            int minutes = (int)(( value - hours )*60);
            return new TimeSpan(0, hours, minutes, 0);
        }

        static TimeSpan GetAddedTime( string input, double time ) {
            var textTime = GetTimeFromText( input );
            return textTime.Add( GetTimeFromDouble( time ) );
        } 

        static void Main( string[] args ) {
            var totalTime = GetAddedTime( "8:30", 8.67 );
            Console.WriteLine( totalTime ); // 17.10
        }
    }
}

Upvotes: 0

InBetween
InBetween

Reputation: 32740

You shouldn't be using a TextBox for this. You have a specific control to take care of dates and times: DateTimePicker.

Add one of these to your form. If you only want the user to modify the time, disallowing date changes, you can do the following:

public myForm()
{
    InitializeComponent();
    ....
    myDateTimePicker.Format = DateTimePickerFormat.Custom;
    myDateTimePicker.CustomFormat = "HH:mm"; //Shows only hours and minutes in 24h format
    myDateTimePicker.Value = DateTime.Now.Date; //sets the time to today at 0:00
    myDateTimePicker.MinDate = DateTime.Now.Date;
    myDateTimePicker.MaxDate = DateTime.Now.Date.Add(new TimeSpan(23, 59, 59)); //User can't change date.
}

private DateTime newTime;
private void myDateTimePicker_ValueChanged(object sender, EventArgs e)
{
    newTime = myDateTimePicker.Value.AddHours(8.5);
}

Obviously if you want the user to set the date too, then simply change the MinValue and MaxValue restrictions to your requirements (if any) and choose one of the predefined format options instead of DateTimePickerFormat.Custom.

Upvotes: 2

Pikoh
Pikoh

Reputation: 7703

You could do something like this:

string input = textbox_1.Text;
DateTime inputTime;
if (DateTime.TryParseExact(input, "HH:mm", CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out inputTime))
{
      DateTime outputTime=inputTime.AddHours(8).AddMinutes(40);
      textbox_2.Text = outputTime.ToString("HH:mm");
}

Basically, you parse the input string to a DateTime variable, add the time you need and then convert back to string to show it in your TextBox

Upvotes: 0

active92
active92

Reputation: 654

You can use DateTime instead of double.

private void button1_Click(object sender, EventArgs e)
    {
        DateTime dateTime = DateTime.ParseExact(textBox1.Text, "HH:mm", System.Globalization.CultureInfo.CurrentCulture);
        TimeSpan time = new TimeSpan(8,40,0);
        DateTime outputTime = dateTime.Add(time);
        textBox2.Text = outputTime.ToString("HH:mm");
    }

Upvotes: 0

Anoop J
Anoop J

Reputation: 267

Try this :

 dateValue.AddHours(8);
 dateValue.AddMinutes(30);

Upvotes: 0

Related Questions