Hi,
I have a DateTimePicker,whose
dateTimePicker1.Format = Time
dateTimePicker1.ShowUpDown = True
But now i want only the hour part from the Date Time Picker as i have to compare the hours with some logic in my Database.

Please Guide how cn i do this?

Dani AI

Generated

Good to know: the picker’s Format/CustomFormat only changes how the time looks; it does not change what is stored in Value. That is why showing hh:00:00 tt still lets minutes sneak into the saved value, as hinted. Also, Value.Hour returns 0..23, so midnight is 0 (not 12) and noon is 12, which explains what @Ehtesham Siddiq saw after trying ’s idea. If you need a 12-hour number for display or storage, format it instead of using .Hour.

To persist exactly the top of the hour with your sentinel date, construct a new DateTime that zeroes minutes and seconds before saving:

Dim v As DateTime = dtp.Value
Dim hourOnly As New DateTime(1900, 1, 1, v.Hour, 0, 0)
' Save hourOnly via a parameterized command

If you prefer to store a 12-hour number and AM/PM separately:

Dim hour12 As Integer = CInt(dtp.Value.ToString("hh"))
Dim meridiem As String = dtp.Value.ToString("tt")

To stop users from ever picking minutes/seconds, snap the control to the hour when it changes:

Private Sub dtp_ValueChanged(...) Handles dtp.ValueChanged
    Dim v = dtp.Value
    If v.Minute <> 0 OrElse v.Second <> 0 OrElse v.Millisecond <> 0 Then
        dtp.Value = New DateTime(v.Year, v.Month, v.Day, v.Hour, 0, 0)
    End If
End Sub

Two database tips: saving DateTimePicker.Text (as @M.Waqas Aslam suggested) is fragile because it is culture-dependent; prefer parameters with DateTime. And if you are on SQL Server 2008+ and only need the time of day, consider a time(0) column and save TimeSpan.FromHours(dtp.Value.Hour); comparisons will be simpler and clearer.

Recommended Answers

All 6 Replies

Try dateTimePicker1.value.hour

Try this,
dateTimePicker1.Format = Custom
dateTimePicker1.ShowUpDown = True
dateTimePicker1.Customformat = h

commented: :) +12

Try this,
dateTimePicker1.Format = Custom
dateTimePicker1.ShowUpDown = True
dateTimePicker1.Customformat = h

With DateTimePicker1
            .Format = DateTimePickerFormat.Custom
            .ShowUpDown = True
            '// .CustomFormat = "h" '// 12Hour.day
            .CustomFormat = "H" '// 24Hour.day
            MsgBox(.Value.Hour)
        End With

Try dateTimePicker1.value.hour

Hi,
acutally i want to save only the Hour part in the Database.I have set the format,custom format.
I have also tried using .hour but when i save i dont get 12 in the Hour value.
I want to save Data in Date column,right now according to my structure im saving data in this manner as shown below:
1/1/1900 10:53:00 AM
but i want to save it in this manner=>1/1/1900 10:00:00 AM
Even if the user selects the time in the Date Time Picker as 10:30:25 AM it should still save as 10:00:00 Am.
Properties of my DateTimePicker are:dtp.CustomFormat=hh
dtp.format=Time
dtp.ShowUpDown=True
Please guide how cn i do this

Hello

U can set the CustomFormat property of the picker to hh:00:00 tt
So that it can take only the hour part and not the minutes and seconds

hi !
set time format custom , hh , and then save like this , datetimepicker.text :)
Regards
M.Waqas Aslam

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.