Round to nearest Multiple? NumericUpDown Question.

M

Mark

Sorry, a bit new at this CF.

I'm looking for a function that will allow me round the min from a
now() function to the nearest 15.

So for example if it's 9:31 I would like it to return 30

i've got TimeID set as a string:
TimeId = FormatDateTime(Now(), DateFormat.ShortTime)

which is working fine. and using mid or right am able to get the min
separated, but the rounding functions seem to just round to whole
numbers.
I'd be fine with a floor or ceiling command too, so that 9:31 would
return either 30 or 45.
I'm using a numericupdown to get the user of the form to input time in
increments of 15min.

And I guess since I'm on the topic is there anyway to make a
NumericUpDown control flip?
Using my original example. if I'm at 45 and press up I'd like it to
revert back to 00.

thanks in advance
 
M

Mark

ahem. i found a workaround solution. i'll post it in case anyone else
ever needs it

MinInt is the cint(right(TimeId,2)
Multiple is the Multiple you are interested in rounding too (in my
case 15)

MinRound = Math.Round(MinInt / Multiple) * Mutiple
 
P

Paul G. Tobey [eMVP]

Just do it yourself. Note that your rounding scheme doesn't round to the
nearest, but it's very easy to handle that:

-----

// Get the data from the text box. This is entered as a minutes value, more
or less like

// what you'd see from the DateTime.Now.

Int32 min = Int32.Parse(textBox1.Text);

// Round the minutes to the nearest 15. I get the number of 15 minute
increments near

// the minutes value here. One, the nearest 15 minute increment below and
the other

// above.

Int32 inc = min / 15;

Int32 inc1 = inc + 1;

Int32 nearest;

Int32 extrahour = 0;

// Select the right increment.

if (min - inc * 15 < inc1 * 15 - min)

{

nearest = inc * 15;

}

else

{

nearest = inc1 * 15;

}

// Now, you have to handle the case where the minutes are 60, since that
doesn't make

// sense

if (nearest == 60)

{

nearest = 0;

extrahour = 1;

}



// Show the minutes number and the extra hour that you need to add to your
time (note that

// this may also cause a wrap to the day, etc., so you have to handle that,
too, to really have

// a solution.

label1.Text = nearest.ToString();

label2.Text = extrahour.ToString();
 
M

Mark

Any idea why my now() statement is returning a time an hour in
advance?
my windows time is correct, is their a regional setting with vs.net
that could be out of whack?
 
P

Paul G. Tobey [eMVP]

*Any* time you're getting a time, from anywhere, that is off by a number of
hours, immediately think "time zone". Whenever the offset is exactly one
hour, think "daylight saving time". You haven't told us what device we're
talking about, when you're making this call, etc., so it's a little hard to
come up with a specific diagnosis. Since the parts of the US that do it are
on DST right now, a first guess might be that either a) the PC to which you
are syncing is not set for automatic DST adjustments or b) the device isn't.
So, to go further, you'll have to tell us what time zone is set on the PC to
which you are syncing, what time zone is set on the device, what OS the
device is running, etc. You should also tell us *anything* that you are
doing that might affect the current local time, the time zone, etc.

Paul T.
 
M

Mark

Sorry. I'm using vs.net 2008's built in Mobile 6.0 emulator at this
point.
My Windows time is set to Mountain Time (GMT-7) with automatically
adjust dst clicked.

It's odd in that i'm not doing anything besides running now() and it
returns everything correct except the time is an hour in the future.
i had forgotten about the fact dst had been slightly moved ahead this
year. I thought the real dst had come and gone as well?

To add to the confusion, I just opened the emulator because I realized
the time is given at the top and that time is correct!

Windows time = correct
emulator time = correct
now() = off by an hour.

i'm puzzled.
 
P

Paul G. Tobey [eMVP]

Double-check the version of the framework (we want the latest). The .NET
CF, unfortunately, does a bunch of stuff that I think is silly behind the
scenes with respect to dates and times. If it just used calls to the OS,
each time you make a corresponding call to the framework function, there
would be no problems. The most-common problem is that, when you change the
date/time/timezone after you start your application, you're stuck, as it
only reads the current time zone information on startup. This could also be
a problem in your case.

Paul T.

Mark said:
Sorry. I'm using vs.net 2008's built in Mobile 6.0 emulator at this
point.
My Windows time is set to Mountain Time (GMT-7) with automatically
adjust dst clicked.

It's odd in that i'm not doing anything besides running now() and it
returns everything correct except the time is an hour in the future.
i had forgotten about the fact dst had been slightly moved ahead this
year. I thought the real dst had come and gone as well?

To add to the confusion, I just opened the emulator because I realized
the time is given at the top and that time is correct!

Windows time = correct
emulator time = correct
now() = off by an hour.

i'm puzzled.

*Any* time you're getting a time, from anywhere, that is off by a number
of
hours, immediately think "time zone". Whenever the offset is exactly one
hour, think "daylight saving time". You haven't told us what device
we're
talking about, when you're making this call, etc., so it's a little hard
to
come up with a specific diagnosis. Since the parts of the US that do it
are
on DST right now, a first guess might be that either a) the PC to which
you
are syncing is not set for automatic DST adjustments or b) the device
isn't.
So, to go further, you'll have to tell us what time zone is set on the PC
to
which you are syncing, what time zone is set on the device, what OS the
device is running, etc. You should also tell us *anything* that you are
doing that might affect the current local time, the time zone, etc.

Paul T.


Any idea why my now() statement is returning a time an hour in
advance?
my windows time is correct, is their a regional setting with vs.net
that could be out of whack?
On Apr 17, 10:14 am, "Paul G. Tobey [eMVP]" <p space tobey no spam AT
no instrument no spam DOT com> wrote:
Just do it yourself. Note that your rounding scheme doesn't round to
the
nearest, but it's very easy to handle that:

// Get the data from the text box. This is entered as a minutes
value,
more
or less like
// what you'd see from the DateTime.Now.
Int32 min = Int32.Parse(textBox1.Text);
// Round the minutes to the nearest 15. I get the number of 15 minute
increments near
// the minutes value here. One, the nearest 15 minute increment below
and
the other
// above.
Int32 inc = min / 15;
Int32 inc1 = inc + 1;
Int32 nearest;
Int32 extrahour = 0;
// Select the right increment.
if (min - inc * 15 < inc1 * 15 - min)

nearest = inc * 15;



nearest = inc1 * 15;

// Now, you have to handle the case where the minutes are 60, since
that
doesn't make
if (nearest == 60)

nearest = 0;
extrahour = 1;

// Show the minutes number and the extra hour that you need to add to
your
time (note that
// this may also cause a wrap to the day, etc., so you have to handle
that,
too, to really have
// a solution.
label1.Text = nearest.ToString();
label2.Text = extrahour.ToString();

Paul T.
Sorry, a bit new at this CF.
I'm looking for a function that will allow me round the min from a
now() function to the nearest 15.
So for example if it's 9:31 I would like it to return 30
i've got TimeID set as a string:
TimeId = FormatDateTime(Now(), DateFormat.ShortTime)
which is working fine. and using mid or right am able to get the min
separated, but the rounding functions seem to just round to whole
numbers.
I'd be fine with a floor or ceiling command too, so that 9:31 would
return either 30 or 45.
I'm using a numericupdown to get the user of the form to input time
in
increments of 15min.
And I guess since I'm on the topic is there anyway to make a
NumericUpDown control flip?
Using my original example. if I'm at 45 and press up I'd like it to
revert back to 00.
thanks in advance
 
M

Mark

Sorry if this is a double post. i was pretty sure i replied.

It's definitely a DST error.

Setting my date to Dec 18 2008 makes the now() statement time correct.
So somewhere along the line one of my devices thinks we're out of DST.

I'm running VS.Net 2008 on Windows XP. The time zone in xp is set to
MST (-7) and adjust for DST is checked.
I'm using the windows mobile 6.0 emulator within visual studio.

The time at the top corner of my emulator is correct.

Very peculiar, definitely related to DST. I know we switched early
this year, but I'm pretty sure the normal day it comes into affect has
occured as well.
 
P

Paul G. Tobey [eMVP]

There are OS updates that have to be applied to make everything work. If
you are automatically updating XP, I'm sure those have happened. It's the
device, including the emulator, that I'm most-concerned was properly
updated, because the updates have to be installed and I'm not convinced that
automatic updates for WM did that.

The Energy Policy Act of 2006 was the law in the stupid US that changed when
Daylight Saving Time occurs. Instead of the second Sunday in April, it was
changed to the last Sunday in March and, instead of the last Sunday in
October, it was changed to the first Sunday in November. Idiots; just
idiots. Everyone knows that it has no positive effect on energy consumption
and costs hundreds of millions of dollars, Euros, and every other currency
in software time. DST is stupid to begin with anyway, but let's change it
around periodically, in case one plan isn't dumb enough.

I love how I've had to become something of an expert on all this time zone
stuff and I live in one of the few areas in the US smart enough to not use
those idiot adjustments. I've looked through the text above for probably an
hour and it's still probably not right, but...

Paul T.

Mark said:
Sorry if this is a double post. i was pretty sure i replied.

It's definitely a DST error.

Setting my date to Dec 18 2008 makes the now() statement time correct.
So somewhere along the line one of my devices thinks we're out of DST.

I'm running VS.Net 2008 on Windows XP. The time zone in xp is set to
MST (-7) and adjust for DST is checked.
I'm using the windows mobile 6.0 emulator within visual studio.

The time at the top corner of my emulator is correct.

Very peculiar, definitely related to DST. I know we switched early
this year, but I'm pretty sure the normal day it comes into affect has
occured as well.


*Any* time you're getting a time, from anywhere, that is off by a number
of
hours, immediately think "time zone". Whenever the offset is exactly one
hour, think "daylight saving time". You haven't told us what device
we're
talking about, when you're making this call, etc., so it's a little hard
to
come up with a specific diagnosis. Since the parts of the US that do it
are
on DST right now, a first guess might be that either a) the PC to which
you
are syncing is not set for automatic DST adjustments or b) the device
isn't.
So, to go further, you'll have to tell us what time zone is set on the PC
to
which you are syncing, what time zone is set on the device, what OS the
device is running, etc. You should also tell us *anything* that you are
doing that might affect the current local time, the time zone, etc.

Paul T.


Any idea why my now() statement is returning a time an hour in
advance?
my windows time is correct, is their a regional setting with vs.net
that could be out of whack?
On Apr 17, 10:14 am, "Paul G. Tobey [eMVP]" <p space tobey no spam AT
no instrument no spam DOT com> wrote:
Just do it yourself. Note that your rounding scheme doesn't round to
the
nearest, but it's very easy to handle that:

// Get the data from the text box. This is entered as a minutes
value,
more
or less like
// what you'd see from the DateTime.Now.
Int32 min = Int32.Parse(textBox1.Text);
// Round the minutes to the nearest 15. I get the number of 15 minute
increments near
// the minutes value here. One, the nearest 15 minute increment below
and
the other
// above.
Int32 inc = min / 15;
Int32 inc1 = inc + 1;
Int32 nearest;
Int32 extrahour = 0;
// Select the right increment.
if (min - inc * 15 < inc1 * 15 - min)

nearest = inc * 15;



nearest = inc1 * 15;

// Now, you have to handle the case where the minutes are 60, since
that
doesn't make
if (nearest == 60)

nearest = 0;
extrahour = 1;

// Show the minutes number and the extra hour that you need to add to
your
time (note that
// this may also cause a wrap to the day, etc., so you have to handle
that,
too, to really have
// a solution.
label1.Text = nearest.ToString();
label2.Text = extrahour.ToString();

Paul T.
Sorry, a bit new at this CF.
I'm looking for a function that will allow me round the min from a
now() function to the nearest 15.
So for example if it's 9:31 I would like it to return 30
i've got TimeID set as a string:
TimeId = FormatDateTime(Now(), DateFormat.ShortTime)
which is working fine. and using mid or right am able to get the min
separated, but the rounding functions seem to just round to whole
numbers.
I'd be fine with a floor or ceiling command too, so that 9:31 would
return either 30 or 45.
I'm using a numericupdown to get the user of the form to input time
in
increments of 15min.
And I guess since I'm on the topic is there anyway to make a
NumericUpDown control flip?
Using my original example. if I'm at 45 and press up I'd like it to
revert back to 00.
thanks in advance
 
M

Mark

i'm using .net compact framework version 3.5
if that helps any?

There are OS updates that have to be applied to make everything work. If
you are automatically updating XP, I'm sure those have happened. It's the
device, including the emulator, that I'm most-concerned was properly
updated, because the updates have to be installed and I'm not convinced that
automatic updates for WM did that.

The Energy Policy Act of 2006 was the law in the stupid US that changed when
Daylight Saving Time occurs. Instead of the second Sunday in April, it was
changed to the last Sunday in March and, instead of the last Sunday in
October, it was changed to the first Sunday in November. Idiots; just
idiots. Everyone knows that it has no positive effect on energy consumption
and costs hundreds of millions of dollars, Euros, and every other currency
in software time. DST is stupid to begin with anyway, but let's change it
around periodically, in case one plan isn't dumb enough.

I love how I've had to become something of an expert on all this time zone
stuff and I live in one of the few areas in the US smart enough to not use
those idiot adjustments. I've looked through the text above for probably an
hour and it's still probably not right, but...

Paul T.


Sorry if this is a double post. i was pretty sure i replied.
It's definitely a DST error.
Setting my date to Dec 18 2008 makes the now() statement time correct.
So somewhere along the line one of my devices thinks we're out of DST.
I'm running VS.Net 2008 on Windows XP. The time zone in xp is set to
MST (-7) and adjust for DST is checked.
I'm using the windows mobile 6.0 emulator within visual studio.
The time at the top corner of my emulator is correct.
Very peculiar, definitely related to DST. I know we switched early
this year, but I'm pretty sure the normal day it comes into affect has
occured as well.
*Any* time you're getting a time, from anywhere, that is off by a number
of
hours, immediately think "time zone". Whenever the offset is exactly one
hour, think "daylight saving time". You haven't told us what device
we're
talking about, when you're making this call, etc., so it's a little hard
to
come up with a specific diagnosis. Since the parts of the US that do it
are
on DST right now, a first guess might be that either a) the PC to which
you
are syncing is not set for automatic DST adjustments or b) the device
isn't.
So, to go further, you'll have to tell us what time zone is set on the PC
to
which you are syncing, what time zone is set on the device, what OS the
device is running, etc. You should also tell us *anything* that you are
doing that might affect the current local time, the time zone, etc.
Paul T.

Any idea why my now() statement is returning a time an hour in
advance?
my windows time is correct, is their a regional setting with vs.net
that could be out of whack?
On Apr 17, 10:14 am, "Paul G. Tobey [eMVP]" <p space tobey no spam AT
no instrument no spam DOT com> wrote:
Just do it yourself. Note that your rounding scheme doesn't round to
the
nearest, but it's very easy to handle that:
-----
// Get the data from the text box. This is entered as a minutes
value,
more
or less like
// what you'd see from the DateTime.Now.
Int32 min = Int32.Parse(textBox1.Text);
// Round the minutes to the nearest 15. I get the number of 15 minute
increments near
// the minutes value here. One, the nearest 15 minute increment below
and
the other
// above.
Int32 inc = min / 15;
Int32 inc1 = inc + 1;
Int32 nearest;
Int32 extrahour = 0;
// Select the right increment.
if (min - inc * 15 < inc1 * 15 - min)
{
nearest = inc * 15;
}
else
{
nearest = inc1 * 15;
}
// Now, you have to handle the case where the minutes are 60, since
that
doesn't make
// sense
if (nearest == 60)
{
nearest = 0;
extrahour = 1;
}
// Show the minutes number and the extra hour that you need to add to
your
time (note that
// this may also cause a wrap to the day, etc., so you have to handle
that,
too, to really have
// a solution.
label1.Text = nearest.ToString();
label2.Text = extrahour.ToString();
-----
Paul T.

Sorry, a bit new at this CF.
I'm looking for a function that will allow me round the min from a
now() function to the nearest 15.
So for example if it's 9:31 I would like it to return 30
i've got TimeID set as a string:
TimeId = FormatDateTime(Now(), DateFormat.ShortTime)
which is working fine. and using mid or right am able to get the min
separated, but the rounding functions seem to just round to whole
numbers.
I'd be fine with a floor or ceiling command too, so that 9:31 would
return either 30 or 45.
I'm using a numericupdown to get the user of the form to input time
in
increments of 15min.
And I guess since I'm on the topic is there anyway to make a
NumericUpDown control flip?
Using my original example. if I'm at 45 and press up I'd like it to
revert back to 00.
thanks in advance
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top