Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

Posted

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

 

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

Posted

What is your goal

 

I can set a custom format in the DateTime picker' date=' 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.[/quote']

 

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?

Posted

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

 

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' date=' 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?[/quote']
  • Leaders
Posted

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?

Iceplug, USN

One of my coworkers thinks that I believe that drawing bullets is the most efficient way of drawing bullets. Whatever!!! :-(

Posted

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:

Posted

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.

 

 

}

Nothing is as illusive as 'the last bug'.
  • Leaders
Posted
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)

 

And if so' date=' 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?[/quote']

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.

Iceplug, USN

One of my coworkers thinks that I believe that drawing bullets is the most efficient way of drawing bullets. Whatever!!! :-(

Posted

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! :)

Posted

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. :)

  • Leaders
Posted
If you can't use #define to replace that, you can always use the text editor Replace to do it. ;)

Iceplug, USN

One of my coworkers thinks that I believe that drawing bullets is the most efficient way of drawing bullets. Whatever!!! :-(

Posted

Be cultural...

 

I come from Visual Foxpro, and you could set a global setting:

Set hours to 24

Hi!

Make your own Culture...

 

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

Posted

<Keanu Reeves Matrix voice> Whoa

 

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! :)

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...