Avatar billede rsa Nybegynder
24. oktober 2002 - 11:09 Der er 6 kommentarer og
1 løsning

Find ugenr, dagnr i ugen, dagens nr. i året p.g.a. dato.

Jeg har en dato (tekstfelt 6 pos.) F.eks. 150303.
Hvordan får jeg ovenstående til rådighed i Visual Basic?
Jeg har har i "oldtiden" lavet dette i cobol, så det må vel også kunne laves i VB.
Eller hvor kan jeg læse omdet?
PÅ forhånd tak!
Avatar billede bennytordrup Nybegynder
24. oktober 2002 - 11:10 #1
Kig på DatePart
Avatar billede bennytordrup Nybegynder
24. oktober 2002 - 11:11 #2
DatePart Function
     

Returns a Variant (Integer) containing the specified part of a given date.

Syntax

DatePart(interval, date[,firstdayofweek[, firstweekofyear]])

The DatePart function syntax has these named arguments:

Part Description
interval Required. String expression that is the interval of time you want to return.
date Required. Variant (Date) value that you want to evaluate.
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


The firstweekofyear argument has these settings:

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 DatePart function to evaluate a date and return a specific interval of time. For example, you might use DatePart to calculate the day of the week or the current hour.

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

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

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

The returned date part is in the time period units of the current Arabic calendar.  For example, if the current calendar is Hijri and the date part to be returned is the year, the year value is a Hijri year.
Avatar billede ocp Nybegynder
24. oktober 2002 - 11:13 #3
Dim sDate As String
    sDate = "150303"
   
    MsgBox Format((CDate(Mid$(sDate, 1, 2) & "/" & Mid$(sDate, 3, 2) & "/" & Mid$(sDate, 5, 2))), "long date")
Avatar billede ocp Nybegynder
24. oktober 2002 - 11:28 #4
Ups... det var ikke det hele:

    Dim sDate As String
    Dim dt As Date
    Dim str As String
   
    sDate = "150303"
   
    dt = CDate(Mid$(sDate, 1, 2) & "/" & Mid$(sDate, 3, 2) & "/" & Mid$(sDate, 5, 2))
   
    str = str & "Date: " & Format(dt, "long date") & vbLf
    str = str & "Weekday: " & Weekday(dt, vbMonday) & " " & WeekdayName(Weekday(dt, vbMonday)) & vbLf
    str = str & "Week: " & Format(dt, "ww") & vbLf
    str = str & "Day of year: " & CLng(dt) - CLng(DateSerial(Year(dt), 1, 1))

    MsgBox str
Avatar billede rsa Nybegynder
24. oktober 2002 - 11:33 #5
Endnu engang tak for hjælpen!
Avatar billede bennytordrup Nybegynder
24. oktober 2002 - 11:34 #6
Dim sDate As String
    Dim dt As Date
    Dim str As String
   
    sDate = "150303"
   
    dt = CDate(Mid$(sDate, 1, 2) & "/" & Mid$(sDate, 3, 2) & "/" & Mid$(sDate, 5, 2))
   
    str = str & "Date: " & Format(dt, "long date") & vbLf
    str = str & "Weekday: " & DatePart("w", dt, vbUseSystem, vbUseSystem) & vbLf
    str = str & "Week: " & DatePart("ww" dt, vbUseSystem, vbUseSystem) & vbLf
    str = str & "Day of year: " & DatePart("y", dt, vbUseSystem, vbUseSystem)

    MsgBox str
Avatar billede bennytordrup Nybegynder
24. oktober 2002 - 11:35 #7
Den løsning, jeg har skrevet, bruger kontrolpanelets indstillinger til at finde ud af dagnummer og ugenummer.
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester