How to get the starting date based on week number in vb6? - vb6

How can I convert the week number to the starting date of that week? Example: week 41 to October 9, 2016? The date must also depend on the year. Example: For the year 2017, week 41 must equal to October 8.
I've already searched and found this (How to get starting date in a Week based on week number using vb.net?) but it is for vb.net. I am not sure if this is applicable in vb6. If this does not work in vb6, how can I do it?
Thanks :)

Try this:
Private Sub Form_Load()
Dim d As Date
d = GetWeekStartDate(41, 2016)
MsgBox d
d = GetWeekStartDate(41, 2017)
MsgBox d
End Sub
Private Function GetWeekStartDate(weekNumber As Integer, year As Integer) As Date
Dim startDate As Date
Dim day As Integer
startDate = DateSerial(year, 1, 1)
day = Weekday(startDate, vbSunday)
startDate = DateAdd("d", DaysToAdd(day), startDate)
GetWeekStartDate = DateAdd("ww", weekNumber - 1, startDate)
End Function
Private Function DaysToAdd(day As Integer) As Integer
DaysToAdd = 0
If day > 1 Then DaysToAdd = 7 - day + 1
End Function

You can simplify this if you don't need all of the options, but it looks about right:
Option Explicit
Private Function WeekDate( _
ByVal Year As Long, _
ByVal Week As Long, _
Optional ByVal FirstWholeWeekIs1 As Boolean, _
Optional ByVal FirstDayOfWeek As VbDayOfWeek = vbUseSystemDayOfWeek) As Date
Dim YearDate As Date
Dim WeekdayOffset As Long
Dim WeekOffset As Long
YearDate = DateSerial(Year, 1, 1)
WeekdayOffset = Weekday(YearDate, FirstDayOfWeek) - 1
If WeekdayOffset <> 0 Then
YearDate = DateAdd("d", -WeekdayOffset, YearDate)
WeekOffset = IIf(FirstWholeWeekIs1, 0, 1)
Else
WeekOffset = 1
End If
WeekDate = DateAdd("ww", Week - WeekOffset, YearDate)
End Function
Private Sub Form_Load()
Dim Year As Long
Dim Week As Long
AutoRedraw = True
Year = 2015
Week = 1
Print Year, Week, WeekDate(Year, Week)
Print Year, Week, WeekDate(Year, Week, FirstWholeWeekIs1:=True)
Week = 2
Print Year, Week, WeekDate(Year, Week)
Print Year, Week, WeekDate(Year, Week, FirstDayOfWeek:=vbMonday)
Year = 2016
Week = 41
Print Year, Week, WeekDate(Year, Week, FirstWholeWeekIs1:=True)
Year = 2017
Week = 1
Print Year, Week, WeekDate(Year, Week, FirstWholeWeekIs1:=False)
Print Year, Week, WeekDate(Year, Week, FirstWholeWeekIs1:=True)
End Sub
If not quite right I'm sure you could make adjustments.

Related

How to calculate the minute in DTPICKER vb6

I'm creating a payroll system I want to calculate the minute late of Employee using two dtpicker
Dtpicker1 is for time in and Dtpicker2 for Timeout
Private Sub calc_Click()
oras = DateDiff("n", DTPicker1, DTPicker2)
Text1.Text = oras
End sub
If all employees are working the same amount of hours (8 hours/day for example):
Private Sub calc_Click()
Dim iWorkdayHours As Integer
Dim iMinutesWorked As Integer
Dim iMinutesLate As Integer
' Get the amount of minutes between two dates
iMinutesWorked = DateDiff("n", DTPicker1, DTPicker2)
' Get number of hours employee should have worked
iWorkdayHours = 8
iMinutesLate = (iWorkdayHours * 60) - iMinutesWorked
If iMinutesLate > 0 Then
Text1.Text = iMinutesLate & " minutes late."
Else
Text1.Text = "On time."
End If
End Sub
If employees have different shift lengths, you can update iWorkdayHours.

How to calculate timedifference in vb6

I want to calculate time difference in vb6. i had been done it in vb.net. but i don't know how to convert in vb6.Because the datetime type will not be available in vb6.
i have pasted my vb.net code below. how to do the same in vb6?
Public Sub timecal()
Dim dFrom As DateTime
Dim dTo As DateTime
Dim tempstarttime As DateTime
Dim idletime As String = "00:05:00"
Dim Needtosub As String = "00:01:00"
Dim timeDiff As String
If DateTime.TryParse(start_time, dFrom) AndAlso DateTime.TryParse(end_time, dTo) Then
Dim TS As TimeSpan = dTo - dFrom
Dim hour As Integer = TS.Hours
Dim mins As Integer = TS.Minutes
Dim secs As Integer = TS.Seconds
timeDiff = ((hour.ToString("00") + ":") + mins.ToString("00") + ":") + secs.ToString("00")
sscheck1 = False
If timeDiff >= idletime Then
tempstarttime = System.DateTime.Parse(end_time)
tempstarttime = tempstarttime.AddMinutes(-1)
start_time = Strings.Format(tempstarttime, "yyyy-MM-dd HH:mm:ss")
sscheck1 = True
End If
End If
End Sub
You can calculate the time difference between two Date values (which will include the time) using the DateDiff function
For difference in minutes:
Dim lMinutes as Long
lMinutes = DateDiff("n", dFrom, dTo)
For difference in seconds:
Dim lSeconds as Long
lSeconds = DateDiff("s", dFrom, dTo)
For difference in hours:
Dim lHours as Long
lHours = DateDiff("h", dFrom, dTo)
For the purposes of getting your string time diff in hours:minutes:seconds, I would do:
Dim lSeconds as Long, lMinutes as Long
lSeconds = DateDiff("s", dFrom, dTo)
lMinutes = Fix(lSeconds / 60) '// Gets the whole number (not rounded)
lSeconds = ((lSeconds / 60) - lMinutes) * 60 '// get the remaining seconds
sTimeDiff = "00:" & Format$(lMinutes, "00") & ":" & Format$(lSeconds, "00")
Note: If lMinutes is greater than 60, you'll need to do similar math to pull out the hours portion before pulling the minutes and seconds. This code assumes your timespan is less than an hour based on your example.
use vb DateDiff Function i.e
Dim datTim1 As Date = #1/4/2001#
Dim datTim2 As Date = #1/9/2001#
' Assume Sunday is specified as first day of the week.
Dim wD As Long = DateDiff(DateInterval.Weekday, datTim1, datTim2)
Dim wY As Long = DateDiff(DateInterval.WeekOfYear, datTim1, datTim2)
or can be done with another general way
Dim dtStart As Long
Dim dtEnd As Long
Dim result As Long
Dim i As Integer
dtStart = GetTickCount
dtEnd = GetTickCount
result = dtEnd – dtStart // miliseconds
OutAddIn.objApptItem.Start = CDbl(result) // miliseconds to date

How to get month and year from the date

How to get month & year from the date
Date = 01/02/2012 (dd/mm/yyyy)
From the above date, i want to get 02/2012 (mm/yyyy)
How to write a code in vb6
Need vb6 code help
You could use the Month() and Year() functions which would take a Date parameter.
I.e. Month(Now) would return the Month and Year(Now) would return the Year.
Dim yourDate As Date
yourDate = #01/02/2012#
Dim yourMonth As Integer
Dim yourYear As Integer
yourMonth = Month(yourDate)
yourYear = Year(yourDate)
You can use Format$:
Dim formattedDate as String
Dim myDate as Date
myDate = #1/2/2012#
formattedDate = Format$(myDate, "mm/yyyy")

Change the week start day on DTPicker

Is it possible to change the Day of the Week that the DTPicker control uses for the first day of the week 'on the fly'?
I know that it uses the system first day of the week (as defined in control panel) for this setting but can it be changed to use another day without changing the control panel setting?
Try this, from a post on the old VB6 newsgroup by MikeD
You can do it with a DTPicker using the Win32 API. The DTPicker uses
an actual MonthView control. You can send this control a MCM_SETFIRSTDAYOFWEEK message to change the first day of the week. Note that you must (and can only) do this
in the DropDown event because prior to that, the MonthView control
doesn't exist. The MonthView gets destroyed after the CloseUp event.
Oh...the value for the first day of week is the lParam of SendMessage
(the wParam is always 0)
Option Explicit
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA"
(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As
Any) As Long
Private Const MCM_FIRST As Long = &H1000&
Private Const MCM_SETFIRSTDAYOFWEEK As Long = (MCM_FIRST + 15)
Private Const DTM_FIRST As Long = &H1000&
Private Const DTM_GETMONTHCAL As Long = (DTM_FIRST + 8)
Private Sub DTPicker1_DropDown()
Dim hMonthview As Long
'Get hwnd of MonthView control
hMonthview = SendMessage(DTPicker1.hwnd, DTM_GETMONTHCAL, 0&, ByVal 0&)
'Set first day of week for MonthView, according to the following:
' Value Day of Week
' 0 Monday
' 1 Tuesday
' 2 Wednesday
' 3 Thursday
' 4 Friday
' 5 Saturday
' 6 Sunday
Call SendMessage(hMonthview, MCM_SETFIRSTDAYOFWEEK, 0&, ByVal 6&) 'first
day of week = Sunday
End Sub

Compare two date times

label1 displays the last transaction date/time which I get from a database through a query. label2 is the system date/time. I have a timer that executes a command button after which I want to check if the date/time in label1 is smaller than 5 minutes. If so then I want to show a massage.
But I don’t know why my code is failing to perform this function.
Any help will be much appreciated.
Private Sub Command1_Click()
Dim date1 As Date
Dim date2 As Date
date1 = Format(Now, "yyyy/mm/dd hh:mm:ss")
date2 = Format(label1, "yyyy/mm/dd hh:mm:ss")
If DateDiff("n", date1, date2) < 2 Then
MsgBox ("Not Vending")
End If
End Sub
I've also tried:
Private Sub Command1_Click()
Dim date1 As Date
Dim label1 As Date
date1 = Format(Now, "yyyy/mm/dd hh:mm:ss")
date2 = label1
If DateDiff("m", Now, date1) > DateDiff("m", Now, label1) Then
MsgBox ("Not Vending")
End If
End Sub
As well as:
Private Sub Command1_Click()
If DateDiff("n", Now, label1) > 5 Then
MsgBox ("Not Vending")
End If
End Sub
If the date pulled from the DB is earlier than Now, DateDiff will always return a negative number if you pass Now as the second parameter. It looks like you're checking for the passage of time, so I'll assume the dates in DB will always be before Now. You need to switch the order of Now and the date to which it is being compared (DateDiff("n", date1, Now) instead of DateDiff("n", Now, date1).
Private Sub Command1_Click()
Dim date1 As Date
date1 = CDate(Label1.Caption)
If DateDiff("n", date1, Now) < 5 Then
MsgBox ("Not Vending")
End If
End Sub
An other way without using the DateDiff-function would be direct calculation with the Date-variables - since under the hood these are actually Doubles.
Private Sub Command1_Click()
Dim date1 As Date
date1 = CDate(Label1.Caption)
'#12:05:00 AM# is representing a value of 5 Minutes in the Date-datatype
If (date1 - Now) < #12:05:00 AM# Then
MsgBox ("Not Vending")
End If
End Sub
If you want to check the time difference you can also use
Private Sub Command1_Click()
Dim date1 As Date
date1 = CDate(Label1.Caption)
'#12:05:00 AM# is representing a value of 5 Minutes in the Date-datatype
If Abs(date1 - Now) < #12:05:00 AM# Then
MsgBox ("Not Vending")
End If
End Sub
Where Abs(date1 - Now) returns the time difference as Date-value
Btw. date1 = Format(Now, "yyyy/mm/dd hh:mm:ss") doesn't make sence. Since Now returns a Date-value, Format converts that Date-value into a String and assigning it to date1 converts that String back to a Date-value ùsing the local system settings for date formats - which means that the program will behave different on different systems.

Resources