Military Time

kirkkelly

Newcomer
Joined
Jan 5, 2005
Messages
2
Location
Texas
Is there any way to alway have the application return military time vice whatever the workstation settings are. I can set a custom format in the DateTime picker, but I need it all the time in my application and I hate to have to code it each time I need to display a date/time value.

Thanks

Kirk
 
Using the Date.ToString subroutine, you can format it however you like, eg:

Visual Basic:
Date.Now.ToString("HH:mm")

That will spit out 24hr time. Not sure what you mean by 'Military Time', but you can make whatever format you want using this method.

Pinky
 
What is your goal

kirkkelly said:
I can set a custom format in the DateTime picker, but I need it all the time in my application and I hate to have to code it each time I need to display a date/time value.

What do you mean by having to code it each time? Do you mean having to code the custom format each time you use the DateTime picker, as opposed to just invoking it without modification? That is, entering it into your code "as is" without parameter modification, thereby cutting down coding time?
 
I come from Visual Foxpro, and you could set a global setting:
Set hours to 24

Then whenever you addressed a time value, anywhere in your application, it was represented in 24 hour format without remember to reformat before you used it.

Kirk

Richard Crist said:
What do you mean by having to code it each time? Do you mean having to code the custom format each time you use the DateTime picker, as opposed to just invoking it without modification? That is, entering it into your code "as is" without parameter modification, thereby cutting down coding time?
 
This sounds an awful lot like an implicit conversion (not something that .NET likes to support, I think).
How do you currently have the displaying of the date coded?
 
Hopeful idea

Iceplug,

Would it be possible to override the date time tostring() method at a high level so that whenever a date time tostring() is done at any level below the override a 24-hour time is returned?

And if so, could a similar override be done to allow the user to enter 24 hour time and have it automatically converted to internal time without having the code need to specify a time format?

With the ultimate behavior of one where no matter where in your code (in that application) you accept from the user or display to the user a time value it always is in a 24 hour time format.

Inquiring newbie minds want to know. :cool:
 
Why not create your own MilitairyDateTime class.

DateTime is sealed so you can't inherit it making it a bit more work, but using the proxy pattern (http://www.dofactory.com/Patterns/PatternProxy.aspx), you can implement most of the methods by just calling the underlaying DateTime object, and only really put some code in the methods that return a time string.

So you'd get something like (by head ;) )
class MilitairyDateTime
{
private DateTime mDateTime

//Implement all the DateTime methods we want to implement

// create all the methods you'll use. e.g. for the Hour property:
public int Hour
{
get
{
return mDateTime.Hour;
}
}

For the methods that return a string, you'd have to do a little more to actually return the correctly modified string.

This would allow you to code the conversion only once. But you'd have a bit more work to create the class itself.


}
 
Richard Crist said:
Would it be possible to override the date time tostring() method at a high level so that whenever a date time tostring() is done at any level below the override a 24-hour time is returned?
No, you cannot change the coding of the DateTime's ToString() method unless you use a different class... and the DTPicker does not return anything besides a DateTime. However, if you are calling ToString(), then it would be of little effort to run a replace on your code document, converting all .ToString() occurences into .ToString("hh:mm") (which is probably much less work than creating an all-new class)

Richard Crist said:
And if so, could a similar override be done to allow the user to enter 24 hour time and have it automatically converted to internal time without having the code need to specify a time format?
Well, you may be able to tweak the time format returned by setting some Global Culture format for the application so that all dates are expressed in 24-hour format. But, as I mentioned, if you are not using .ToString() then you must be using an implicit conversion, such as MyString = MyDate, which Option Strict does not allow. And I don't know how to do this Culture format thing either.
 
Another question

Thank you for the information, Iceplug.

I had also looked at the culture thing, contemplating perhaps defining a custom one. But I decided that would prove impractical since I don't yet even understand using culture stuff to begin with. :p

I have studied the sealed thingy in the C# manual, and now I see it's implications.

I have another question. In 3GL programming (read standard C) you can issue compliler directives to #DEFINE blahblah differentblahblah. Can you do the same in .NET C#? I suppose I'm thinking about making it easy to do something like every time you call DateTime.ToString() it is replaced by your DateTime.ToString("hh:mm") before compilation. In other words, something like:

#DEFINE DateTime.ToString() DateTime.ToString("hh:mm")

or something like that. I'm using something like a lot because I'm not sure exactly how to phrase some of this stuff.

Thank you! :)
 
A bear with me question

Uhm, bear with me here, 'cause I'm a .NET newbie (although not quite as new as I was some months ago). :cool:

You are saying that we can "recreate" the DateTime picker by defining a class MilitaryTime that "uses" it by define a variable of that type, and then we are responsible for duplicating any DateTime methods, properties, etc. that we will be using? And then we can call MilitaryTime.DateTime object to do cool stuff we coded?

Be gentle. :)
 
Be cultural...

kirkkelly said:
I come from Visual Foxpro, and you could set a global setting:
Set hours to 24
Hi!
Make your own Culture...

Code:
Sub Main
...
Application.CurrentCulture = MyCulture()
...
End Suub

Private Function MyCulture() As System.Globalization.CultureInfo
	Dim ciMy As System.Globalization.CultureInfo = System.Globalization.CultureInfo.CreateSpecificCulture("")

	ciMy.DateTimeFormat.ShortDatePattern = "yyyy-MM-dd"
	ciMy.NumberFormat.NegativeSign = "-"
	ciMy.NumberFormat.NumberDecimalDigits = 2
	ciMy.NumberFormat.NumberDecimalSeparator = "."
	ciMy.NumberFormat.NumberGroupSeparator = ""
	ciMy.NumberFormat.NumberGroupSizes = New Integer() {0}
	ciMy.NumberFormat.PercentDecimalDigits = 2
	ciMy.NumberFormat.PercentDecimalSeparator = "."
	ciMy.NumberFormat.PercentGroupSeparator = ""
	ciMy.NumberFormat.PercentGroupSizes = New Integer() {0}
	Return ciMy
End Function

Then you're pretty sure that you'll get things displayed as you like it

nb. There are some ancient VB methods/functions that still default to en-us but you'll find them soon enough.

HTH
/Kejpa
 
<Keanu Reeves Matrix voice> Whoa

kejpa said:
Hi!
Make your own Culture...

Outstanding! I will make much use of this in the future, because there is always a need to do the least work to accomplish the greatest result. A man that worked at my first job always said, "The greatest ideas come from those who are too lazy to do the work, and smart enough to figure a way around it." Thanks for the information! :)
 
Back
Top