How to determine the time interval in a DateTimePicker

I have a DateTimePicker element in the form specified like this:

dtpEntry.Format = DateTimePickerFormat.Custom; dtpEntry.CustomFormat = "dd/MM/yyyy hh:mm:ss"; dtpEntry.ShowUpDown = true; 

I would like the user to only be able to increase or decrease time in increments of 5 minutes.

Any suggestions on how to do this?

+4
source share
6 answers

This is possible by observing the ValueChanged event and overriding the value. This sample form worked well:

 public partial class Form1 : Form { public Form1() { InitializeComponent(); dateTimePicker1.CustomFormat = "dd/MM/yyyy hh:mm"; dateTimePicker1.Format = DateTimePickerFormat.Custom; dateTimePicker1.ShowUpDown = true; dateTimePicker1.Value = DateTime.Now.Date.AddHours(DateTime.Now.Hour); mPrevDate = dateTimePicker1.Value; dateTimePicker1.ValueChanged += new EventHandler(dateTimePicker1_ValueChanged); } private DateTime mPrevDate; private bool mBusy; private void dateTimePicker1_ValueChanged(object sender, EventArgs e) { if (!mBusy) { mBusy = true; DateTime dt = dateTimePicker1.Value; if ((dt.Minute * 60 + dt.Second) % 300 != 0) { TimeSpan diff = dt - mPrevDate; if (diff.Ticks < 0) dateTimePicker1.Value = mPrevDate.AddMinutes(-5); else dateTimePicker1.Value = mPrevDate.AddMinutes(5); } mBusy = false; } mPrevDate = dateTimePicker1.Value; } } 
+7
source

I slightly modified the answer from SixThree to fix the error discovered by Necromporph. It should be like this:

in class

 private DateTime prevTimePicker1; private bool navigatingDateTimePicker = false; 

in the constructor

 prevTimePicker1 = dateTimePickerStart.Value; navigatingDateTimePicker = false; 

event

 private void dateTimePickerStart_ValueChanged(object sender, EventArgs e) { if (!navigatingDateTimePicker) { /* First set the navigating flag to true so this method doesn't get called again while updating */ navigatingDateTimePicker = true; /* using timespan because that the only way I know how to round times well */ TimeSpan tempTS = dateTimePickerStart.Value - dateTimePickerStart.Value.Date; TimeSpan roundedTimeSpan; TimeSpan TDBug = dateTimePickerStart.Value - prevTimePicker1; if (TDBug.TotalMinutes == 59) { // first: if we are going back and skipping an hour it needs an adjustment roundedTimeSpan = TimeSpan.FromMinutes(5 * Math.Floor((tempTS.TotalMinutes - 60) / 5)); dateTimePickerStart.Value = dateTimePickerStart.Value.Date + roundedTimeSpan; } else if (dateTimePickerStart.Value > prevTimePicker1) { // round up to the nearest interval roundedTimeSpan = TimeSpan.FromMinutes(5 * Math.Ceiling(tempTS.TotalMinutes / 5)); dateTimePickerStart.Value = dateTimePickerStart.Value.Date + roundedTimeSpan; } else { // round down to the nearest interval from prev roundedTimeSpan = TimeSpan.FromMinutes(5 * Math.Floor(tempTS.TotalMinutes / 5)); dateTimePickerStart.Value = dateTimePickerStart.Value.Date + roundedTimeSpan; } navigatingDateTimePicker = false; } prevTimePicker1 = dateTimePickerStart.Value; } 
+5
source

The problem is that the up / down control automatically increases or decreases the currently selected part of the date / time selection (i.e. year / month / day / hour / etc.).

You are probably better off adding your own up / down control (possibly a very small vscrollbar) located right next to the date and time selector and plugging it in to increase / decrease five-minute intervals from the date / time value.

+1
source

Or just try the following:

 private void dateTimePicker1_ValueChanged(object sender, EventArgs e) { if (this.dateTimePicker1.Value.Minute % 5 == 0) return; if (this.dateTimePicker1.Value.Minute % 5 == 1) this.dateTimePicker1.Value = this.dateTimePicker1.Value.AddMinutes(4); if (this.dateTimePicker1.Value.Minute % 5 == 4) this.dateTimePicker1.Value = this.dateTimePicker1.Value.AddMinutes(-4); } 
+1
source

I know this is an old article, but I created a better solution to this problem based on the answer above.

in class

 private DateTime prevTimePicker1; private bool navigatingDateTimePicker = false; 

in the constructor

 prevTimePicker1 = dateTimePickerStart.Value; navigatingDateTimePicker = false; 

event

 private void dateTimePickerStart_ValueChanged(object sender, EventArgs e) { if (!navigatingDateTimePicker) { /* First set the navigating flag to true so this method doesn't get called again while updating */ navigatingDateTimePicker = true; /* using timespan because that the only way I know how to round times well */ TimeSpan tempTS = dateTimePickerStart.Value - dateTimePickerStart.Value.Date; TimeSpan roundedTimeSpan; if (dateTimePickerStart.Value > prevTimePicker1) { // round up to the nearest interval roundedTimeSpan = TimeSpan.FromMinutes(5 * Math.Ceiling(tempTS.TotalMinutes / 5)); dateTimePickerStart.Value = dateTimePickerStart.Value.Date + roundedTimeSpan; } else { // round down to the nearest interval from prev roundedTimeSpan = TimeSpan.FromMinutes(5 * Math.Floor(tempTS.TotalMinutes / 5)); dateTimePickerStart.Value = dateTimePickerStart.Value.Date + roundedTimeSpan; } navigatingDateTimePicker = false; } prevTimePicker1 = dateTimePickerStart.Value; } 
0
source

you can add this code

 int minuteDiff = dtpJamAppointmentDokter.Value.Minute - prevTimePicker1.Minute; if (minuteDiff == 59) { dtpJamAppointmentDokter.Value = dtpJamAppointmentDokter.Value.AddHours(-1); } 

front

 TimeSpan tempTS = dtpJamAppointmentDokter.Value - dtpJamAppointmentDokter.Value.Date; 
0
source

All Articles