Scenario: Trying to find out the End Week Number(current year's week number) and Start Week Number using a given period. Suppose PeriodWeeks = 10. That means from today's week number to last 10 weeks which surely will go to last year in current situation.
Code I have:
perdiodWeeks = 10 ' this is a constant in the code
periodMonths= periodQtrs * 3 ' this calculates month from given number of quarters
endDate = DateAdd("m",-1,Date)
endYear = DatePart("yyyy", endDate)
endMonth = DatePart("m", endDate)
startDate = DateAdd("m", -(periodMonths-1), endDate)
startYear = DatePart("yyyy", startDate)
startMonth = DatePart("m", startDate)
How can I calculate the following?
startYW ' start year week number
endYW ' end year week number
where value would be endYW = 201506 and startYW = perdiodWeeks from endYW. 06 is the current Week Number of 2015
Calculate the initial date (substract #weeks*7) and use the available functions to retrieve the required information
Option Explicit
Dim periodWeeks
periodWeeks = 10
Dim dateToday, dateStart
dateToday = Now()
dateStart = DateAdd("d",periodWeeks*-7, dateToday)
Dim weekToday, weekStart
weekToday = DatePart("ww",dateToday)
weekStart = DatePart("ww",dateStart)
Related
I am trying to create a simple script that prompts the user for their birthday, formatted as 10.02.20, then takes that string and turns it into text, such as October 2nd 2020. I have the following code:
Dim bd, message, title ' define variables
title = "What is your birthday?" ' set variable
message = "Format like so: 12.15.07; 3.03.05" ' set variable
bd = InputBox(message, title) ' prompt user
Dim year
year = bd.Substring(6, 2)
Dim month
month = bd.Substring(0, 2)
Dim day
day = bd.Substring(3, 2)
msgbox bd ' for testing
call msgbox(year + month + day) 'also testing
And I am getting an error after the prompt, ...\Desktop\test.vbs(8, 1) Microsoft VBScript runtime error: Object required: '12.23.03' and I am not sure what it means, Object Required.
Any fixes or suggestions would be very much appreciated.
You cannot use Substring in VBScript. You can use Mid instead:
Dim year
year = Mid(bd, 7, 2)
The first character in a string is at position 1, not 0, so I adjusted your parameter from 6 to 7.
Also, to concatenate strings, although + works, you can also use &:
call msgbox(year & month & day)
You can use this function in vbscript : FormatDateTime(date,format)
Parameter Description
date Required.
Any valid date expression (like Date() or Now())
format Optional.
A value that specifies the date/time format to use can take the following values:
0 = vbGeneralDate - Default. Returns date: mm/dd/yyyy and time if specified: hh:mm:ss PM/AM.
1 = vbLongDate - Returns date: weekday, monthname, year
2 = vbShortDate - Returns date: mm/dd/yyyy
3 = vbLongTime - Returns time: hh:mm:ss PM/AM
4 = vbShortTime - Return time: hh:mm
Dim Title,Input
Title = "format date"
Input = InputBox("Enter your date of Birthday !",Title,"10.02.20")
Input = FormatDateTime(Replace(Input,".","/"),1)
MsgBox Input
I would like to know what could be the best way to obtain the starting date values for each month based on the date range.
For example: If I am given a year range of 2015-11-10 and 2018-01-15(format YYYY-mm-dd). Then I would like to extract following dates:
2015-12-01
2016-01-01
.
.
2018-01-01
You can try to use this flow for generating the first day of each month in the provided date range.
Overall flow
Step 1 Configuration: Start
Step 2 Configuration: Configure Date Range
Provide the start and end dates as configuration parameters via this step.
Step 3 Configuration: Generate First Dates For Months
This uses a Groovy script, which is provided below
Groovy script
flowFile = session.get();
if(!flowFile)
return;
DATE_FORMAT = 'yyyy-MM-dd';
startDate = Date.parse(DATE_FORMAT, flowFile.getAttribute("start_date"));
endDate = Date.parse(DATE_FORMAT, flowFile.getAttribute("end_date"));
allFirstDates = "";
Calendar calendar = Calendar.getInstance();
Set firstDaysOfMonths = new LinkedHashSet();
for (int i = 0; i <= endDate-startDate; i++) {
calendar.setTime(startDate.plus(i));
calendar.set(Calendar.DAY_OF_MONTH, 1);
firstDayOfMonth = calendar.getTime();
if (firstDayOfMonth.compareTo(startDate) >= 0) {
firstDaysOfMonths.add(calendar.getTime().format(DATE_FORMAT));
}
}
firstDaysOfMonths.each {
firstDayOfMonth -> allFirstDates = allFirstDates + firstDayOfMonth + "\n";
}
flowFile = session.putAttribute(flowFile,"all_first_dates", allFirstDates );
session.transfer(flowFile,REL_SUCCESS)
Step 4 Configuration: View Result
Output of run:
When the flow is run, the attribute all_first_dates will be populated with the first dates of each month in the date range.
I'm trying to create a bi-weekly appointment using vbscript. But for some reason the output date is always occurring on every other Thursday when it should be every other Friday. I have outlook set up to start the week on a Sunday. I have changed and removed.DayOfWeekMask and it stays on Thursdays.
Dim endDate
Dim startDate
endDate = #01/01/2022#
startDate = #01/01/2012#
Const olRecursBiWeekly = 1
Const olFriday = 6
Set olkEvent = olkApp.CreateItem(1)
olkEvent.Subject = "Pay Day"
olkEvent.AllDayEvent = True
olkEvent.ReminderSet = False
Set objRecurrence = olkEvent.GetRecurrencePattern
objRecurrence.DayOfWeekMask = olFriday
objRecurrence.RecurrenceType = olRecursBiWeekly
objRecurrence.PatternStartDate = startDate
objRecurrence.Interval = 2
objRecurrence.PatternEndDate = endDate
olkEvent.Save
Thanks for your help.
There no such thing is olRecursBiWeekly. You need olRecursWeekly (=1).
olFriday is &H20 (32 decimal).
You need to move things around:
The DayOfWeekMask should be set after the RecurrenceType property has
been set and before the PatternEndDate and PatternStartDate properties
are set.
From MSDN
Has anyone a working function that returns the week number of a given date in vb6?
This used to work:
Dim W As Integer
W = Format(DateSerial(2010, 1, 1), "ww", vbMonday, vbFirstFourDays)
But in Windows 8.1 you now get "Out of stack space".
The answer is a bit more complex given the offsets
The following function should work well for what you need. Simply pass it a valid Date object, and it will return an Integer of the Week number.
Private Function Week(dteValue As Date) As Integer
'Monday is set as first day of week
Dim lngDate As Long
Dim intWeek As Integer
'If january 1. is later then thursday, january 1. is not in week 1
If Not Weekday("01/01/" & Year(dteValue), vbMonday) > 4 Then
intWeek = 1
Else
intWeek = 0
End If
'Sets long-value for january 1.
lngDate = CLng(CDate("01/01/" & Year(dteValue)))
'Finds the first monday of year
lngDate = lngDate + (8 - Weekday("01/01/" & Year(dteValue), vbMonday))
'Increases week by week until set date is passed
While Not lngDate > CLng(CDate(dteValue))
intWeek = intWeek + 1
lngDate = lngDate + 7
Wend
'If the date set is not in week 1, this finds latest week previous year
If intWeek = 0 Then
intWeek = Week("31/12/" & Year(dteValue) - 1)
End If
Week = intWeek
End Function
This code is courtesy of FreeVBCode.com (included a reference to give attribute to the original author).
I am currently working on defining workflow scripts in HP Application Lifecycle management tool using VB Script.
My problem is I have to generate an Excel chart for calculating the efforts put on by various developers in a particular Sprint(timeframe). In process of generating the same my condition is to eliminate the weekend dates(ie., Saturday and Sunday) from the chart. If the Sprint startdate falls on a weekend the date shoud automatically be initialized to the next immediate monday and if the Sprint End date falls on a weekend the end date should be displayed as the Friday that just passed. I have validated these two conditions. I am trying to shave off the Saturday and Sundays that come inbetween these two days. Please help me on the same. Also please let me know if you need any inputs from my side.
#Sabaresh, I believe this is what you are looking for.
Tip: See this answer for information on downloading Microsoft's authoritative WSH reference as a Windows help file.
Option Explicit
Dim dCandidateDate, dActualStartDate, dActualEndDate
dCandidateDate = CDate("2012/08/18")
dActualStartDate = SprintStartDate(dCandidateDate)
dCandidateDate = CDate("2012/09/16")
dActualEndDate = SprintEndDate(dCandidateDate)
WScript.Echo "Sprint date range: " _
& dActualStartDate & " through " & dActualEndDate
'
' Return following Monday if dCandidateDate is
' Saturday or Sunday.
'
Function SprintStartDate(dCandidateDate)
Dim nWeekday : nWeekday = DatePart("w", dCandidateDate)
Select Case nWeekday
Case 7 ' Saturday
SprintStartDate = DateAdd("d", 2, dCandidateDate)
Case 1 ' Sunday
SprintStartDate = DateAdd("d", 1, dCandidateDate)
Case Else
SprintStartDate = dCandidateDate
End Select
End Function
'
' Return previous Friday if dCandidateDate is
' Saturday or Sunday.
'
Function SprintEndDate(dCandidateDate)
Dim nWeekday : nWeekday = DatePart("w", dCandidateDate)
Select Case nWeekday
Case 7 ' Saturday
SprintEndDate = DateAdd("d", -1, dCandidateDate)
Case 1 ' Sunday
SprintEndDate = DateAdd("d", -2, dCandidateDate)
Case Else
SprintEndDate = dCandidateDate
End Select
End Function