Toon posts:

[VB] Tijd optellen

Pagina: 1
Acties:

Verwijderd

Topicstarter
Gegroet mede aardbewoners,

Ik heb geprobeerd om de tijd van een textvak op te tellen bij een ander textvak.
Dit lukt echter niet.
Het resultaat is steeds "1". Ik heb al op google groups gezocht maar niets gevonden. Ook de verschillende boeken die ik heb over VB praten niet over tijdsberekeningen.
Is er iemand die weet waar ik deze info wel kan vinden ?

Text2.Text = Format$(Now, "HH:MM")
Text7.Text = Format$("HH:MM")
tekst3 = Text2.Text + Text7.Text


tekst3 is de variabele welke na een druk op de knop naar de printer gestuurd wordt.

  • YellowCube
  • Registratie: December 2001
  • Laatst online: 15:47

YellowCube

Wait...what?

je wilt weten hoeveel uur er tussen 2 waarden zit neem ik aan.
Volgens mij moet je de functie DateDiff gebruiken:
DateDiff Function


Returns a Variant (Long) specifying the number of time intervals between two specified dates.

Syntax

DateDiff(interval, date1, date2[, firstdayofweek[, firstweekofyear]])

The DateDiff function syntax has these named arguments:

Part Description
interval Required. String expression that is the interval of time you use to calculate the difference between date1 and date2.
date1, date2 Required; Variant (Date). Two dates you want to use in the calculation.
firstdayofweek Optional. A constant that specifies the first day of the week. If not specified, Sunday is assumed.
firstweekofyear Optional. A constant that specifies the first week of the year. If not specified, the first week is assumed to be the week in which January 1 occurs.


Settings

The interval argument has these settings:

Setting Description
yyyy Year
q Quarter
m Month
y Day of year
d Day
w Weekday
ww Week
h Hour
n Minute
s Second


The firstdayofweek argument has these settings:

Constant Value Description
vbUseSystem 0 Use the NLS API setting.
vbSunday 1 Sunday (default)
vbMonday 2 Monday
vbTuesday 3 Tuesday
vbWednesday 4 Wednesday
vbThursday 5 Thursday
vbFriday 6 Friday
vbSaturday 7 Saturday


Constant Value Description
vbUseSystem 0 Use the NLS API setting.
vbFirstJan1 1 Start with week in which January 1 occurs (default).
vbFirstFourDays 2 Start with the first week that has at least four days in the new year.
vbFirstFullWeek 3 Start with first full week of the year.


Remarks

You can use the DateDiff function to determine how many specified time intervals exist between two dates. For example, you might use DateDiff to calculate the number of days between two dates, or the number of weeks between today and the end of the year.

To calculate the number of days between date1 and date2, you can use either Day of year ("y") or Day ("d"). When interval is Weekday ("w"), DateDiff returns the number of weeks between the two dates. If date1 falls on a Monday, DateDiff counts the number of Mondays until date2. It counts date2 but not date1. If interval is Week ("ww"), however, the DateDiff function returns the number of calendar weeks between the two dates. It counts the number of Sundays between date1 and date2. DateDiff counts date2 if it falls on a Sunday; but it doesn't count date1, even if it does fall on a Sunday.

If date1 refers to a later point in time than date2, the DateDiff function returns a negative number.

The firstdayofweek argument affects calculations that use the "w" and "ww" interval symbols.

If date1 or date2 is a date literal, the specified year becomes a permanent part of that date. However, if date1 or date2 is enclosed in double quotation marks (" "), and you omit the year, the current year is inserted in your code each time the date1 or date2 expression is evaluated. This makes it possible to write code that can be used in different years.

When comparing December 31 to January 1 of the immediately succeeding year, DateDiff for Year ("yyyy") returns 1 even though only a day has elapsed.

Note For date1 and date2, if the Calendar property setting is Gregorian, the supplied date must be Gregorian. If the calendar is Hijri, the supplied date must be Hijri.

My doctor says that I have a malformed public-duty gland and a natural deficiency in moral fibre and that I am therefore excused from saving the world


  • Vincenz0
  • Registratie: Augustus 2006
  • Laatst online: 25-09 14:06

Vincenz0

Coder

wat je nu doet is 2 strings optellen, dan bereken je weinig he ;)
ik ben een c# programmeur dus kan geen VB maar ik denk dat je er wel uit komt met dit:
je zult gebruik moeten maken van integers en op gaan tellen bijv:
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// variables(integers):
private Int32 MM1   //minuten 1e tijd
private Int32 MM2   //minuten huidige tijd
private Int32 min   //minuten die je wilt opschrijven in tekst 3
private Int32 uur   // uren die je wilt opschrijven in tekst 3
private Int32 UU1   //uren 1e tijd
private Int32 UU2   //uren huidige tijd
private Int32 days   // dagen

MM2 = DateTime.Now.Minute();
HH2 = DateTime.Now.Hour();

uur = UU1+UU2;
min = MM1 + MM2;

// example code:

if(min == >60) 
{
min - 60; 
uur + 1;
};

if(uur == >24)
{
uur - 24;
days + 1;
};

tekst3 = Convert.ToString(uur) + ":" + Convert.ToString(min);


hopenlijk snap je het een beetje en kan je het vertalen naar VB ;)

[ Voor 60% gewijzigd door Vincenz0 op 19-12-2006 12:23 ]

Coding 4 Fun!


Verwijderd

Topicstarter
@Vincenz0

Ik heb het voor elkaar gekregen met jouw C# voorbeeld. Helemaal perfect.
Echter krijg ik na de dubbelepunt een ongewenste spatie. Ik krijg deze er ook niet uit met b.v. Trim$

HH = Left$(Text2.Text, 2)
MM = Right$(Text2.Text, 2)
HH1 = Left$(Text7.Text, 2)
MM1 = Right$(Text7.Text, 2)

cu = (HH + HH1)
cm = (MM + MM1)
If cm >= 60 Then
cm = cm - 60
cu = cu + 1
End If
cm = Right$(cm, 2)
nieuwetijd = Str$(cu) & ":" & Str$(cm)
Text8.Text = nieuwetijd


Heb jij nog een goed idee om van deze spatie af te komen ? ?

  • j.nolles
  • Registratie: Oktober 2006
  • Laatst online: 18-11 11:30
Volgens mij haal je integers en strings behoorlijk door elkaar heen

En zo kom je van de spatie af
code:
1
nieuwetijd = Trim(Str(cu)) & ":" & Trim(Str(cm))

  • Shuisman
  • Registratie: Maart 2004
  • Laatst online: 01-09 10:12
Er is gewoon een functie DateAdd, dat lijkt me een stukje handiger of niet ?!

Kan nog veel simpeler als je ze echt als date definieert namely:

Dim a As Date
Dim b As Date
Dim c As Date

a = #1:31:00 AM#
b = #2:04:00 AM#
c = a + b

werkt perfect !

[ Voor 55% gewijzigd door Shuisman op 20-12-2006 12:55 ]


  • F_J_K
  • Registratie: Juni 2001
  • Niet online

F_J_K

Moderator CSA/PB

Front verplichte underscores

CSA -> Programming + kleine titeledit per het policy aldaar.

'Multiple exclamation marks,' he went on, shaking his head, 'are a sure sign of a diseased mind' (Terry Pratchett, Eric)


Verwijderd

Topicstarter
Aan iedereen bedankt voor alle info. Het werkt nu perfect. En ik weet dat alles veel beter kan maar ik ben gewoon maar een eenvoudige ziel die wat met VB klooit. (gaande weg wordt dat wel beter)

Greetz,
Emile
Pagina: 1