Problem with dates and times

filleke

New member
Joined
Jan 18, 2006
Messages
4
Location
Belgium
Programming Experience
1-3
Hello,

i'm having a problem which is probably very simple but i can't seem to find the solution.

I have Oracle database field where i need to store a date. The format is "dd-mon-yyyy hh24:mi".

I select the date from a dateTimePicker and then i add the time from a combobox (cboTime) value.

VB.NET:
dateSelected = Format(CDate(dtpProgDatum.Value), "dd-MMM-yyyy")

dateStart = dateSelected & " " & cboTime.Text
Then I need to insert "dateStart" into the database. No problem.
But I also need to insert dateEnd. And that's the problem, I have a value (like 95 minutes, it's an integer) that I need to add to the time from the startDate.

Like: dateStart = 31/jan/2006 17:00
Now I need dateEnd = 31/jan/2006 18:35

Does anyone know how I can do this?
 
Using the TimeSpan Class you can a specific amount of minutes to your time using the following

VB.NET:
Dim Span as new TimeSpan(0,0,95,0)

Then simply add it to it.

VB.NET:
YourTimeSpan.Add(span)

Where 'YourTimeSpan' is in DateTime Format

NB There may be a more simple way but this is how i do it.
 
Last edited:
This is how I would add 10 minutes to the current time.

VB.NET:
        Dim k As Integer = 10 ' this is your minutes integer
        Dim enddate As DateTime
        enddate = DateTime.Now.AddMinutes(k)
 
Whilst DaveT Macktool's example will work, it is limited. For generic times you should use the timespan class. And as an added advantage it's a bit faster too.
 
vis781 said:
Whilst DaveT Macktool's example will work, it is limited. For generic times you should use the timespan class. And as an added advantage it's a bit faster too.
There is no request for a generic time. The request is for two specific DateTime objects, one 95 minutes later than the other. That's what the DateTime.AddMinutes method is for.
 
Also, calling Format and passing a DateTime object is creating a String. If you are storing DateTimes in a database then forget about format. Format is only an issue when DISPLYING dates. All objects are still stored inaxactly the same way regardless of the display format. For instance, in .NET every DateTime object has information for year, month, day, hour, minute, second and millisecond. A date stored in a database is the same and you can't change that.
 
jmcilhinney said:
There is no request for a generic time. The request is for two specific DateTime objects, one 95 minutes later than the other. That's what the DateTime.AddMinutes method is for.

The DateTime.Addminutes method calls datetime/DatePart functions in the background, it's more cumbersome and isn't as exact as the TimeSpan Structure. The TimeSpan structure does it's calculations in ticks and returns a double, which is far more exact that datetime.add minutes.
 
vis781 said:
The DateTime.Addminutes method calls datetime/DatePart functions in the background
Are you sure about that. I could be wrong but I very much doubt it. I'm betting that DateTime.AddMinutes is implemented like this:
VB.NET:
Public Function AddMinutes(ByVal minutes As Integer) As DateTime
    Return Me.Add(TimeSpan.FromMinutes(95))
End Function
If you're going to use a TimeSpan then you should use the FromMinutes method yourself rather than a constructor as it is more obvious exactly what it's doing (self-documenting). Using the AddMinutes method is more intuitive again and, as I said, I very much doubt that it isn't implemented using a TimeSpan anyway. To say that it is less accurate seems a bit ridiculous. Do you really mean to say that the DateTime object returned might not be what it's supposed to be if you use AddMinutes, et al?
 
These aren't my assessments i must admit, just what i read on a site whilst i was doing some reasearch into the most efficient way to accomplish date/time calculations.
 
When in doubt, use Reflector.:D

Add(TimeSpan.FromMinutes)
VB.NET:
[COLOR=black][FONT=Courier New][COLOR=blue]Public[/COLOR] [COLOR=blue]Function[/COLOR] Add([COLOR=blue]ByVal[/COLOR] value [COLOR=blue]As[/COLOR] TimeSpan) [COLOR=blue]As[/COLOR] DateTime
    [COLOR=blue]Return[/COLOR] [COLOR=blue]Me[/COLOR].AddTicks(value._ticks)
[COLOR=blue]End[/COLOR] [COLOR=blue]Function[/COLOR]
 
[COLOR=blue]Public[/COLOR] [COLOR=blue]Shared[/COLOR] [COLOR=blue]Function[/COLOR] FromMinutes([COLOR=blue]ByVal[/COLOR] value [COLOR=blue]As[/COLOR] [COLOR=blue]Double[/COLOR]) [COLOR=blue]As[/COLOR] TimeSpan
    [COLOR=blue]Return[/COLOR] TimeSpan.Interval(value, 60000)
[COLOR=blue]End[/COLOR] [COLOR=blue]Function[/COLOR]
 
[COLOR=blue]Private[/COLOR] [COLOR=blue]Shared[/COLOR] [COLOR=blue]Function[/COLOR] Interval([COLOR=blue]ByVal[/COLOR] value [COLOR=blue]As[/COLOR] [COLOR=blue]Double[/COLOR], [COLOR=blue]ByVal[/COLOR] scale [COLOR=blue]As[/COLOR] [COLOR=blue]Integer[/COLOR]) [COLOR=blue]As[/COLOR] TimeSpan
    [COLOR=blue]If[/COLOR] [COLOR=blue]Double[/COLOR].IsNaN(value) [COLOR=blue]Then[/COLOR]
        [COLOR=blue]Throw[/COLOR] [COLOR=blue]New[/COLOR] ArgumentException(Environment.GetResourceString([COLOR=maroon]"Arg_CannotBeNaN"[/COLOR]))
    [COLOR=blue]End[/COLOR] [COLOR=blue]If[/COLOR]
    [COLOR=blue]Dim[/COLOR] num1 [COLOR=blue]As[/COLOR] [COLOR=blue]Double[/COLOR] = (value * scale)
    [COLOR=blue]Dim[/COLOR] num2 [COLOR=blue]As[/COLOR] [COLOR=blue]Double[/COLOR] = (num1 + IIf((value >= 0), 0.5, -0.5))
    [COLOR=blue]If[/COLOR] ((num2 > 922337203685477) [COLOR=blue]OrElse[/COLOR] (num2 < -922337203685477)) [COLOR=blue]Then[/COLOR]
        [COLOR=blue]Throw[/COLOR] [COLOR=blue]New[/COLOR] OverflowException(Environment.GetResourceString([COLOR=maroon]"Overflow_TimeSpanTooLong"[/COLOR]))
    [COLOR=blue]End[/COLOR] [COLOR=blue]If[/COLOR]
    [COLOR=blue]Return[/COLOR] [COLOR=blue]New[/COLOR] TimeSpan(([COLOR=blue]CType[/COLOR](num2, [COLOR=blue]Long[/COLOR]) * 10000))
[COLOR=blue]End[/COLOR] [COLOR=blue]Function[/COLOR]
 
[COLOR=blue]Public[/COLOR] [COLOR=blue]Sub[/COLOR] [COLOR=blue]New[/COLOR]([COLOR=blue]ByVal[/COLOR] ticks [COLOR=blue]As[/COLOR] [COLOR=blue]Long[/COLOR])
    [COLOR=blue]Me[/COLOR]._ticks = ticks
[COLOR=blue]End[/COLOR] [COLOR=blue]Sub[/COLOR]
 
[/FONT][/COLOR]

AddMinutes
VB.NET:
[COLOR=#1000a0]
[COLOR=black][FONT=Courier New][COLOR=blue]Public[/COLOR] [COLOR=blue]Function[/COLOR] AddMinutes([COLOR=blue]ByVal[/COLOR] value [COLOR=blue]As[/COLOR] [COLOR=blue]Double[/COLOR]) [COLOR=blue]As[/COLOR] DateTime
    [COLOR=blue]Return[/COLOR] [COLOR=blue]Me[/COLOR].Add(value, 60000)
[COLOR=blue]End[/COLOR] [COLOR=blue]Function[/COLOR]
 
[COLOR=blue]Private[/COLOR] [COLOR=blue]Function[/COLOR] Add([COLOR=blue]ByVal[/COLOR] value [COLOR=blue]As[/COLOR] [COLOR=blue]Double[/COLOR], [COLOR=blue]ByVal[/COLOR] scale [COLOR=blue]As[/COLOR] [COLOR=blue]Integer[/COLOR]) [COLOR=blue]As[/COLOR] DateTime
    [COLOR=blue]Dim[/COLOR] num1 [COLOR=blue]As[/COLOR] [COLOR=blue]Long[/COLOR] = [COLOR=blue]CType[/COLOR](((value * scale) + IIf((value >= 0), 0.5, -0.5)), [COLOR=blue]Long[/COLOR])
    [COLOR=blue]If[/COLOR] ((num1 <= -315537897600000) [COLOR=blue]OrElse[/COLOR] (num1 >= 315537897600000)) [COLOR=blue]Then[/COLOR]
        [COLOR=blue]Throw[/COLOR] [COLOR=blue]New[/COLOR] ArgumentOutOfRangeException([COLOR=maroon]"value"[/COLOR], _
            Environment.GetResourceString([COLOR=maroon]"ArgumentOutOfRange_AddValue"[/COLOR]))
    [COLOR=blue]End[/COLOR] [COLOR=blue]If[/COLOR]
    [COLOR=blue]Return[/COLOR] [COLOR=blue]Me[/COLOR].AddTicks((num1 * 10000))
[COLOR=blue]End[/COLOR] [COLOR=blue]Function[/COLOR]
[/FONT][/COLOR]
[/COLOR]

So both go to AddTicks
VB.NET:
[COLOR=black][FONT=Courier New][COLOR=blue]Public[/COLOR] [COLOR=blue]Function[/COLOR] AddTicks([COLOR=blue]ByVal[/COLOR] value [COLOR=blue]As[/COLOR] [COLOR=blue]Long[/COLOR]) [COLOR=blue]As[/COLOR] DateTime
    [COLOR=blue]Dim[/COLOR] num1 [COLOR=blue]As[/COLOR] [COLOR=blue]Long[/COLOR] = [COLOR=blue]Me[/COLOR].InternalTicks
    [COLOR=blue]If[/COLOR] ((value > (3155378975999999999 - num1)) [COLOR=blue]OrElse[/COLOR] (value < -num1)) [COLOR=blue]Then[/COLOR]
        [COLOR=blue]Throw[/COLOR] [COLOR=blue]New[/COLOR] ArgumentOutOfRangeException([COLOR=maroon]"value"[/COLOR], _
          Environment.GetResourceString([COLOR=maroon]"ArgumentOutOfRange_DateArithmetic"[/COLOR]))
    [COLOR=blue]End[/COLOR] [COLOR=blue]If[/COLOR]
    [COLOR=blue]Return[/COLOR] [COLOR=blue]New[/COLOR] DateTime(((num1 + value) [COLOR=blue]Or[/COLOR] [COLOR=blue]Me[/COLOR].InternalKind))
[COLOR=blue]End[/COLOR] [COLOR=blue]Function[/COLOR]
[/FONT][/COLOR]
 
Back
Top