How to show a DateTime value as 24 hour?

I am curious if there is a way to show a DateTime value as 24 hour.  I know I can take a string formatted as 24 hour and turn it into a DateTime value, but it then converts it to standard time.

If I have a string = "5/01/2009 23:42:15" and I do a DateTime.Parse() on this value I get
#5/01/2009 11:42:15 PM#.  Is there a way for me to get #5/01/2009 23:42:15 PM# or veen better
#5/01/2009 23:42:15# without the PM or AM...?
LVL 12
rgn2121Asked:
Who is Participating?
 
oobaylyCommented:
Honestly, I wouldn't bother about the debugging issue. It's not like your users will ever see it. It's just the way the VS represents dates internally.
To format a DateTime object as a string, just use:

However, (Like you suggested) I'd store the date values in the datatable as a DateTime so you can sort by them. Then set the Formatting for the Column in your Grid, so the date can bre represented however you wish.
Dim d As DateTime = DateTime.Now
Dim s As String = d.ToString("dd/MM/yyyy HH:mm:ss")

Open in new window

0
 
bob_the_builderCommented:
Hi, have a read through this, it might help.....?

http://www.astahost.com/info.php/question-about-asp-now-function_t7186.html

Cheers,

Bob
0
 
oobaylyCommented:
Internally, a DateTime is represented using the format like this #5/01/2009 11:42:15 PM# (M/dd/yyyy hh:mm:ss tt)

However, if you call ToString() on it, you should get the System's default DateTime format, as specified in Regional and Language Options
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
rgn2121Author Commented:
Looks like all that does it what I already know and that is formatting it into a String that shows it as 24 hour.  I am curious if there is a way so that when my DateTime variable gets the date that when I hover over it in debug mode it shows me a date denoted by ## and shows it in 24 hour notation.
I am using the dates in DataTable and I am converting back anf forth from strings to dates when I have to do anything with Time where sorting and filtering are concerned.  I would like to store them as dates and when they are displayed on reports and in Grids, they will show as 24 hour.
0
 
rgn2121Author Commented:
So if I do somethine like what I have below and dump that to a datatable, then it will show in a grid as 24 hour time?
 
 

dim d as New DateTime
d = formatdatetime("5/01/2009 22:35:12")

Open in new window

0
 
rgn2121Author Commented:
I will be away from this until later this afternoon, but I will follow up..
0
 
Jorge PaulinoIT Pro/DeveloperCommented:
What kind of grid you want to show that information with that format ?
0
 
rgn2121Author Commented:
I currently just dump all data, about 2500 rows and 40 columns, to a DataGridView.  It really is used just while the application is in development right now.  I might add something in later to make it part of the app in a logical place, but for now I just use it to check the data.
I use crystal reports though for all my reporting and I need to be able to show these DateTime values as just the time portion of HH:mm:ss.  The reports are generated nightly so the actual date portion is understood by the user...and witht he amount of data I need to fit on a report, I don't really have room to show a bunch of identical dates.
oobayly>>  Thanks for the code, That is what I currently use to store them in the tables now.  I really wanted to store them as dates for the easy of filtering and such.  I will have to look at the formatting for the columns...
I am not sure if there is a format available to alter the dates in Crystal, but I think I can work around it by making it a string...
Thoughts?
0
 
rgn2121Author Commented:
Maybe I should add my systems default is not 24hour...I am going through a copy of my app now removing where I formatted the dates as strings...
0
 
rgn2121Author Commented:
Okay...I think I can go with the dates, but I do have one issue.  declare and instantiate a new DateTime object, I set it's value to 1001,01,01,0,0,0.  I thought, I guess incorrectly, that DateTime.MinValue was #01/01/1001 00:00:00# as opposed to just #12:00:00 AM#.
I have a class that I created that has numerous DateTime objects.  When they are initially created they get the value of 1001,01,01,0,0,0.  I have a function I use to clear all of the variables in my class after it hase been added to a DataTable.  In that function I se the values of my DateTime objects to DateTime.MinValue.  This way I can check for it later they same way I was setting the string values to String.Empty and using String.IsNullorEmpty().
I know that I can return 01/01/1001 as a string from DateTime.MinValue by using the ToShortDateString(), but I want it as a date since that is what my variables are now.
0
 
rgn2121Author Commented:
Seems like DateTime.MaxValue gives me a Date and a Time where as DateTime.MinValue just gives me a Time...
0
 
rgn2121Author Commented:
Sorry for all the posts...I work nights.
 
0
 
rgn2121Author Commented:
I have gotten around the DateTime.MinValue by using
DateTime.Parse("01/01/1001 00:00:00") in its place.
0
 
Jorge PaulinoIT Pro/DeveloperCommented:
Now you have confused me!
If you have the records defined as datetime in the database and you want to show that in the datagridview with your custom format you can do something like this:

DataGridView1.Columns("MyColumn").DefaultCellStyle.Format = "dd/MM/yyyy HH:mm:ss"
Or have I understood it wrongly ?
0
 
rgn2121Author Commented:
Sorry...I have worked through this a lot tonight, so you can disregard the comments about the format.  Going off of  oobayly's comment about formatting the columns of the grid, I finally found what you just listed above.
I am getting ready to get into the Crystal Reports and correct them to work with these dates instead of the strings I was using before.
The problem I mentioned about the DateTime.MinValue I think I have fixed.  If you had trouble understanding that portion I can explain what I meant and we can see if what I did was a good idea.  Seems very logical and the data is accurate so I think I am good...
0
 
rgn2121Author Commented:
I am going to go ahead and award points as I think the original question has been handled.  I will explain what I did if either of you care, other wise I will post a new question if I have issues with the CR.
Thanks for all the help...I learned a lot, which is what I was hoping for...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.