[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to show a DateTime value as 24 hour?

Posted on 2009-05-01
16
Medium Priority
?
698 Views
Last Modified: 2013-11-26
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...?
0
Comment
Question by:rgn2121
  • 11
  • 2
  • 2
  • +1
16 Comments
 
LVL 10

Expert Comment

by:bob_the_builder
ID: 24277705
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
 
LVL 15

Assisted Solution

by:oobayly
oobayly earned 1500 total points
ID: 24277725
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
 
LVL 12

Author Comment

by:rgn2121
ID: 24277729
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 12

Author Comment

by:rgn2121
ID: 24277734
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
 
LVL 12

Author Comment

by:rgn2121
ID: 24277735
I will be away from this until later this afternoon, but I will follow up..
0
 
LVL 15

Accepted Solution

by:
oobayly earned 1500 total points
ID: 24277758
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
 
LVL 48

Expert Comment

by:jpaulino
ID: 24277876
What kind of grid you want to show that information with that format ?
0
 
LVL 12

Author Comment

by:rgn2121
ID: 24284585
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
 
LVL 12

Author Comment

by:rgn2121
ID: 24284816
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
 
LVL 12

Author Comment

by:rgn2121
ID: 24284888
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
 
LVL 12

Author Comment

by:rgn2121
ID: 24284890
Seems like DateTime.MaxValue gives me a Date and a Time where as DateTime.MinValue just gives me a Time...
0
 
LVL 12

Author Comment

by:rgn2121
ID: 24285006
Sorry for all the posts...I work nights.
 
0
 
LVL 12

Author Comment

by:rgn2121
ID: 24285291
I have gotten around the DateTime.MinValue by using
DateTime.Parse("01/01/1001 00:00:00") in its place.
0
 
LVL 48

Assisted Solution

by:jpaulino
jpaulino earned 500 total points
ID: 24285386
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
 
LVL 12

Author Comment

by:rgn2121
ID: 24285444
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
 
LVL 12

Author Comment

by:rgn2121
ID: 24285450
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question