Click here to Skip to main content
15,890,741 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
how to convert military time to standard by below code ?

What I have tried:

C#
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");
}
Posted
Updated 23-Jun-17 3:55am
Comments
Jochen Arndt 23-Jun-17 9:24am    
I guess you mean Z(ulu) time which is just UTC (no time zone and DST).

So you should use the keyword "UTC" when searching to find what you need because your code snippet does not contain any information about the used datetime format.

But theres is a general rule when using datetimes:
Store them using binary datetimes in UTC and do as much processing as possible with those binary values. Only when displaying convert using the local time zone and format. Similar for user input where the string input must be in a known format and a known time zone (usually the local settings). If possible use the existing picker controls that return values already in binary format.

1 solution

Learn to write readable code. There are at least 4 things that make your code hard to read:
#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 TryParse and 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!

Here is the refactored code that does essentially the same things as your (except that I test for bad inputs):

C#
DateTime amtimein, amtimeout;
if (!DateTime.TryParse(txt1.Text + "AM", out amtimein))
{
	// Warn user that hour # 1 is not valid...
}
else if (!DateTime.TryParse(txt2.Text + "AM", out amtimeout))
{
	// Warn user that hour # 2 is not valid...
}
else
{
	TimeSpan diff = amtimeout.Subtract(amtimein);
	
	if (diff < TimeSpan.Zero)
	{
		diff += TimeSpan.FromHours(12);
	}

	var txtm.Text = diff.ToString(@"hh\:mm");
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900