ich habe zwei Zeiten gegeben. z. B. 12:23 und 23:34
nun soll ich die Zeitdifferenz in Stunden berechnen
da gibt es doch schon fertige Klassen die ich nutzen kann - irgendwelche Tipps?
Du wandelst deine Zeiten einfach in ein DateTime um. Zwei oder mehrere DateTime-Objekte können addiert, subtrahiert etc. werden. In deinem Falle einfach den ersten Wert vom zweiten abziehen. Das resultierende Object ist vom Typ TimeSpan.
DateTime dt1 = DateTime.Parse("14:54");
DateTime dt2 = DateTime.Parse("15:15");
TimeSpan ts = dt2 - dt1;
Console.WriteLine(ts.TotalSeconds.ToString());
.NET GUI - Die Community für grafische Oberflächen unter .NET
Jetzt kostenlos besorgen: .NET BlogBook
Norbert Eder
DasBackup
danke!
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace Arbeitszeit
{
/// <summary>
/// Zusammenfassung für Form1.
/// </summary>
public class FormWorktime : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBoxBegin;
private System.Windows.Forms.TextBox textBoxPauseBegin;
private System.Windows.Forms.TextBox textBoxPauseEnd;
private System.Windows.Forms.TextBox textBoxEnd;
private System.Windows.Forms.Button buttonCalculate;
private System.Windows.Forms.Label labelWorkTime;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
/// <summary>
/// Erforderliche Designervariable.
/// </summary>
private System.ComponentModel.Container components = null;
public FormWorktime()
{
//
// Erforderlich für die Windows Form-Designerunterstützung
//
InitializeComponent();
//
// TODO: Fügen Sie den Konstruktorcode nach dem Aufruf von InitializeComponent hinzu
//
}
/// <summary>
/// Die verwendeten Ressourcen bereinigen.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Vom Windows Form-Designer generierter Code
/// <summary>
/// Erforderliche Methode für die Designerunterstützung.
/// Der Inhalt der Methode darf nicht mit dem Code-Editor geändert werden.
/// </summary>
private void InitializeComponent()
{
this.textBoxBegin = new System.Windows.Forms.TextBox();
this.textBoxPauseBegin = new System.Windows.Forms.TextBox();
this.textBoxPauseEnd = new System.Windows.Forms.TextBox();
this.textBoxEnd = new System.Windows.Forms.TextBox();
this.buttonCalculate = new System.Windows.Forms.Button();
this.labelWorkTime = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// textBoxBegin
//
this.textBoxBegin.Location = new System.Drawing.Point(128, 24);
this.textBoxBegin.Name = "textBoxBegin";
this.textBoxBegin.Size = new System.Drawing.Size(96, 20);
this.textBoxBegin.TabIndex = 0;
this.textBoxBegin.Text = "";
//
// textBoxPauseBegin
//
this.textBoxPauseBegin.Location = new System.Drawing.Point(128, 48);
this.textBoxPauseBegin.Name = "textBoxPauseBegin";
this.textBoxPauseBegin.Size = new System.Drawing.Size(96, 20);
this.textBoxPauseBegin.TabIndex = 1;
this.textBoxPauseBegin.Text = "";
//
// textBoxPauseEnd
//
this.textBoxPauseEnd.Location = new System.Drawing.Point(128, 72);
this.textBoxPauseEnd.Name = "textBoxPauseEnd";
this.textBoxPauseEnd.Size = new System.Drawing.Size(96, 20);
this.textBoxPauseEnd.TabIndex = 2;
this.textBoxPauseEnd.Text = "";
//
// textBoxEnd
//
this.textBoxEnd.Location = new System.Drawing.Point(128, 96);
this.textBoxEnd.Name = "textBoxEnd";
this.textBoxEnd.Size = new System.Drawing.Size(96, 20);
this.textBoxEnd.TabIndex = 3;
this.textBoxEnd.Text = "";
//
// buttonCalculate
//
this.buttonCalculate.Location = new System.Drawing.Point(136, 128);
this.buttonCalculate.Name = "buttonCalculate";
this.buttonCalculate.Size = new System.Drawing.Size(96, 24);
this.buttonCalculate.TabIndex = 4;
this.buttonCalculate.Text = "Berechnen";
this.buttonCalculate.Click += new System.EventHandler(this.buttonCalculate_Click);
//
// labelWorkTime
//
this.labelWorkTime.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.labelWorkTime.Location = new System.Drawing.Point(16, 128);
this.labelWorkTime.Name = "labelWorkTime";
this.labelWorkTime.Size = new System.Drawing.Size(104, 24);
this.labelWorkTime.TabIndex = 5;
//
// label1
//
this.label1.Location = new System.Drawing.Point(16, 24);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(64, 16);
this.label1.TabIndex = 6;
this.label1.Text = "Beginn";
//
// label2
//
this.label2.Location = new System.Drawing.Point(16, 96);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(80, 16);
this.label2.TabIndex = 7;
this.label2.Text = "Ende";
//
// label3
//
this.label3.Location = new System.Drawing.Point(16, 48);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(96, 16);
this.label3.TabIndex = 8;
this.label3.Text = "Dienstlich Gehen";
//
// label4
//
this.label4.Location = new System.Drawing.Point(16, 72);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(104, 16);
this.label4.TabIndex = 9;
this.label4.Text = "Dienstlich Kommen";
//
// FormWorktime
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(248, 174);
this.Controls.Add(this.label4);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.labelWorkTime);
this.Controls.Add(this.buttonCalculate);
this.Controls.Add(this.textBoxEnd);
this.Controls.Add(this.textBoxPauseEnd);
this.Controls.Add(this.textBoxPauseBegin);
this.Controls.Add(this.textBoxBegin);
this.Name = "FormWorktime";
this.Text = "Arbeitszeit";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// Der Haupteinstiegspunkt für die Anwendung.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new FormWorktime());
}
private void buttonCalculate_Click(object sender, System.EventArgs e)
{
DateTime dt1 = DateTime.Parse(this.textBoxBegin.Text);
DateTime dt2 = DateTime.Parse(this.textBoxPauseBegin.Text);
TimeSpan ts = dt2 - dt1;
DateTime dt3 = DateTime.Parse(this.textBoxPauseEnd.Text);
DateTime dt4 = DateTime.Parse(this.textBoxEnd.Text);
TimeSpan ts2 = dt4 - dt3;
TimeSpan workTime = ts + ts2;
this.labelWorkTime.Text = workTime.TotalHours.ToString();
}
}
}