Click here to Skip to main content
15,888,113 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi

I am Using Date time picker to display the time in hr AND MIN

I want the datetime picker to show the hour and time in such a way that hour should increment by 1 and min should increment by 15(eg: 0,15,30,45)

This is so far i got
DTPendtime.CustomFormat = "HH:mm"//where DTPendtime is datetime picker
        DTPendtime.Format = DateTimePickerFormat.Custom
        DTPendtime.ShowUpDown = True

Private Sub DTPendtime_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DTPendtime.ValueChanged
      txtendtime.Text = DTPendtime.Text
  End Sub
Posted

This the VB version:

VB
Private Sub DateTimePicker1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePicker1.ValueChanged
    Dim d As DateTime = DateTimePicker1.Value
    Dim i As Integer = d.Minute Mod 15
    If (i > 0) Then
        DateTimePicker1.Value = DateTimePicker1.Value.AddMinutes(15 - i).AddSeconds(-d.Second)
    End If
End Sub
 
Share this answer
 
Try something like this:

C#
private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
    var diff = dateTimePicker1.Value.TimeOfDay.Minutes % 15;
    if (diff != 0)
    {
        dateTimePicker1.Value = dateTimePicker1.Value.AddMinutes(15 - diff);
    }
}
 
Share this answer
 
Thanks For the Reply dmageiras811

Your post was really helpful
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900