DateTime ottimein, ottimeout;
DateTime pmtimein, pmtimeout;
DateTime amtimein, amtimeout;
DateTime extra;
amtimein = DateTime.Parse(txt1.Text + "AM");
amtimeout = DateTime.Parse(txt2.Text + "AM");
pmtimein = DateTime.Parse(txt3.Text + "PM");
pmtimeout = DateTime.Parse(txt4.Text + "PM");
ottimein = DateTime.Parse(txt5.Text + "PM");
ottimeout = DateTime.Parse(txt6.Text + "PM");
extra = DateTime.Parse(txtExtra.Text + "AM").AddHours(12);
TimeSpan ts = new TimeSpan(0);
var am = amtimein;
var amout = amtimeout;
TimeSpan diff = amout.Subtract(am);
var pmin = pmtimein;
var pmout = pmtimeout;
TimeSpan diffpm = pmtimeout.Subtract(pmtimein);
var otin = ottimein;
var otout = ottimeout;
TimeSpan diffot = ottimeout.Subtract(ottimein);
if (diff < ts)
{
var res = diff + TimeSpan.FromHours(12);
var res_s= diffpm;
var res_r=diffot;
var _s = res + res_s + res_r;
txtm.Text = res.ToString(@"hh\mm");
}
else
{
var res = diff;
var res_s = diffpm;
var res_r = diffot;
var _s = res + res_s + res_r;
txtm.Text = res.ToString(@"hh\mm");
}
Loading
Abhilash J APosted Jun 23, 2017, 11:56 AM
#1 You don't use indentation
#2 You don't add some extra blank lines at appropriate locations.
#3 You declare variable you never use
#4 You have many variable that duplicate other variables.
Also:
* You have to handle wrong user inputs by using
TryParseand take the appropriate action when the input is bad.* In real application, you have to take into account the fact that user might want to enter time in 24 hours format too.
* You probably does not correctly handle extra time that ends between 12:00AM and 12:59AM inclusively as you systematically add 12 hours.
* The code for each case would be similar so you should have functions that handle common code.
* How extra is intended to be use is not clear as all other cases appears to work in pairs... but for extra you systematically add 12 hours.
* Your output format is probably not the expected one. You probably want to use
@"hh\:mm"instead.*
TimeSpan(0)is useless as there is already a property for that:TimeSpan.Zero. If make the code more efficient and more readable.From your question is is not clear what is the desired output format. It seems that you want to output a time span so the military vs standard time does not seems to fit your code!
Abhilash J APosted Jun 23, 2017, 9:27 AM