0

I have a string 10/13/2016 21:42

Dim ETAtime1 As String = Convert.ToDateTime("10/13/2016 21:42").ToString("MM/dd/yyyy HH:mm")
'result is 10/13/2016 21:42

I want the result to be 10/13/2016 21:42 (24 hour format) like the above string. But why, after I have converted it like below, does it become 12 hour format?

 Dim ETAtime As DateTime = DateTime.ParseExact(ETAtime1, "MM/dd/yyyy HH:mm", Nothing)
'result is 10/13/2016 09:42

How to resolve it, it become 09:42 not 21:42? I need DateTime type data not an string.

djv
  • 15,168
  • 7
  • 48
  • 72
Bcktr
  • 208
  • 1
  • 8
  • 22
  • 2
    Why have you tagged this as C# when it's in VB? Note that in the second line, the result is a `DateTime`, not a `String`... a `DateTime` doesn't *have* an inherent format. – Jon Skeet Oct 13 '16 at 14:54
  • I'm so sorry, just I'm able to read C# answer. I can remove tag. – Bcktr Oct 13 '16 at 14:55
  • 3
    Basically, the answer is "You appear to be mistaking what happens when you call `ToString()` on a `DateTime` with data that's in the `DateTime` itself." – Jon Skeet Oct 13 '16 at 14:56
  • If you output the value of `ETAtime.Hour`, you'll see that the hour count is actually 21 as well. `ETAtime.ToString("MM/dd/yyyy HH:mm")` will probably have the value you're expecting to see. Good luck – KSib Oct 13 '16 at 15:01

3 Answers3

2

A DateTime object doesn't have a "format", think of it as just holding what the date and time actually are. Storing that information in a DateTime object is what allows you to perform mathematical/logical operations on it. How you display the DateTime hours to the user is up to you. The ToString method can display either of those hour formats depending on what format string you pass it...for example:

    Dim ETAtime1 As String = Convert.ToDateTime("10/13/2016 21:42").ToString("MM/dd/yyyy HH:mm")
    Dim ETAtime As DateTime = DateTime.ParseExact(ETAtime1, "MM/dd/yyyy HH:mm", Nothing)

    MsgBox(ETAtime.ToString("HH:mm"))
    'Outputs 21:42
    MsgBox(ETAtime.ToString("h:mm"))
    'Outputs 9:42
soohoonigan
  • 2,342
  • 2
  • 10
  • 18
  • Hi, thank you. I was try you code. Its work in message box. But if in breakpoint is different, also database is same. I confused – Bcktr Oct 13 '16 at 15:33
  • If you're "peeking" at the ETAtime variable while debugging, it's just going to show you a default "ToString" visualization of the date and time that are stored in the variable, you have no control over changing that...that's just how visual studio has been programmed to show you the value of that variable. The example above proves that, because even if your breakpoint shows you a different format, when you messagebox the datetime with .tostring you can have it formatted however you'd like. – soohoonigan Oct 13 '16 at 15:43
1

This line takes your string, 10/13/2016 21:42 and parses it using the format provided. This is useful when a date string is in an ambiguous format such as 06/05/2016 and even though this could either represent June 5th or May 6th, but you know that it's day/month, May 6th in my example. For more see DateTime.ParseExact.

Dim ETAtime As DateTime = DateTime.ParseExact(ETAtime1, "MM/dd/yyyy HH:mm", Nothing)

The result of this method is a DateTime, not a string.

'result is 10/13/2016 09:42

So the result is valid - depending how you are inspecting it - however there is no AM/PM indicator. It is already a DateTime. Further operations on ETAtime can prove this.

Dim ETAtime1 As String = Convert.ToDateTime("10/13/2016 21:42").ToString("MM/dd/yyyy HH:mm")
Dim ETAtime As DateTime = DateTime.ParseExact(ETAtime1, "MM/dd/yyyy HH:mm", Nothing)
Console.WriteLine("{0:MM/dd/yyyy HH:mm}", ETAtime)
Console.WriteLine("{0:MM/dd/yyyy hh:mm}", ETAtime)
Console.WriteLine("{0:MM/dd/yyyy hh:mm tt}", ETAtime)

Output

10/13/2016 21:42
10/13/2016 09:42
10/13/2016 09:42 PM

There is really no issue with your code.

My IDE (Visual Studio 2012) displays the date in this format when debugging 10/13/2016 09:42 PM. I am located in the USA. What is displayed should be based on your regional settings. It would be interesting to see a screenshot of yours.

enter image description here

djv
  • 15,168
  • 7
  • 48
  • 72
  • I was try Dim W As DateTime = ETAtime.ToString("MM/dd/yyyy HH:mm") it still have an output "10/13/2016 19:42" I see it in the breakpoint and saved to database with that value, its crazy, it should be "10/13/2016 21:42". Any sugestion? Thank you in advance. – Bcktr Oct 13 '16 at 15:32
  • Are you saving to a String field in the database, or a DateTime field? – djv Oct 13 '16 at 15:33
  • Absolutely yes, the type data is datetime – Bcktr Oct 13 '16 at 15:34
  • As soohoonigan said, *A DateTime object doesn't have a "format"*. What you are seeing is just how your time is being displayed to you. As I demonstrated in my example, you can still parse your instance as "HH:mm" and 21:42 is properly displayed. Your IDE or SQL browser is just displaying it to you as "hh:mm". Where exactly are you seeing the output? – djv Oct 13 '16 at 15:49
  • See my edit. Can you provide a screenshot of your IDE showing the incorrect format? – djv Oct 13 '16 at 16:02
  • @Bcktr what was the problem in the end? – djv Oct 14 '16 at 14:07
0

try this :

      dddd, MMMM d, yyyy HH:mm:ss tt"
irantutors
  • 11
  • 2