vbscript asp - Find every Thursday in a given month - date

I'm trying to find all Thursdays within a given Month using VBScript.
Can anyone help?

Here is one way;
base_date = cdate("21 aug 2011")
'get 1st thursday;
thurs = dateserial(year(base_date), month(base_date), 1)
if (weekday(thurs) <> 1) then thurs = 5 - weekday(thurs) + thurs
'loop subsequent;
do until month(thurs) <> month(base_date)
msgbox thurs
thurs = dateadd("d", 7, thurs)
loop

While the accepted answer does the job it's overly complicated for something that can be accomplished with WeekDay() function and a For loop.
Dim day
Dim startdate: startdate = CDate("21 aug 2011")
Dim enddate
'Get first day of month.
startdate = DateSerial(Year(startdate), Month(startdate), 1)
'Get last day of month.
enddate = DateAdd("m", 1, startdate) - 1
For day = startdate To enddate
If WeekDay(day) = vbThursday Then WScript.Echo day & " = " & WeekDayName(WeekDay(day))
Next
Output:
04/08/2011 = Thursday
11/08/2011 = Thursday
18/08/2011 = Thursday
25/08/2011 = Thursday
Any Date and Time constant can be used here to look for different or multiple weekdays with a little tinkering.

Related

convert date format yywwd to dd-mm-yy

In Access, I would like to convert a date column in format yywwd to dd-mm-yy. (weekday nr. 1 is monday, and years can only from 2000 and later, so e.g. today (monday 15-06-2020) would be 20251 what I would like to be converted to 15-06-2020.
I'm not much of a coder so honestly asside from messing with Datepart I have not tried a whole lot. Does anyone have suggestion?
It seems that the function 'GetDayFromWeekNumber' mentioned here vba convert week number (and year) to date? could work but how is this used in MSAccess?
Thanks a lot in advance!
I have a function that works in Excel to convert YYWWD to a date, this should be very similar if not identical to the code needed in Access. It is quite verbose so you could probably make it simpler, but at least the calculation steps are clearly set out.
The function assumes the ISO definition of week number - i.e. the first week of the year is the week in which the 4th of Jan falls. The first day of a week is Monday, the last day of a week is Sunday.
Function dateFromYYWWD(yywwd)
Dim sYYWWD As String
Dim sYYYY As String
Dim ww As Integer
Dim d As Integer
Dim fourthOfJan As Date
Dim fourthOfJanWeekday As Integer
Dim week1StartDate As Date
Dim targetWeekStartDate As Date
Dim targetDate As Date
' Convert to string if not already
sYYWWD = "" & yywwd
' Get the year in full
sYYYY = "20" + Left(sYYWWD, 2)
' Get the week number and day in the week
ww = CInt(Mid(sYYWWD, 3, 2))
d = CInt(Right(sYYWWD, 1))
' Calculate the date of 4th Jan in the same year
fourthOfJan = CDate(sYYYY & "-01-04")
' Get the day of week of the 4th Jan
' NOTE - CALCULATES MONDAY AS DAY 1 OF THE WEEK, SUNDAY AS DAY 7
fourthOfJanWeekday = Weekday(fourthOfJan, vbMonday)
' Date of the first day of week #1 in the target year
week1StartDate = fourthOfJan - fourthOfJanWeekday + 1
' First day of the target week
targetWeekStartDate = week1StartDate + (ww - 1) * 7
' Target date
targetDate = targetWeekStartDate + d - 1
dateFromYYWWD = targetDate
End Function
It's as simple as this:
Public Function ConvertFromYYWWD(s) As Date
Dim t&
t = DateSerial(2000 + Mid(s, 1, 2), 1, 1) + 7 * (Mid(s, 3, 2) - 1)
ConvertFromYYWWD = t - Weekday(t, vbMonday) + Mid(s, 5, 1)
End Function
Just place the above function in a code module in the database project.
You mentioned in the comments under your question that the week number is always two digits. I am assuming the the year number is likewise always two digits.
The first task is to split the value:
YWDDate = 20251
Year = YWDDate \ 1000 + 2000
2020
Week = (YWDDate Mod 1000) \ 10
25
Weekday = YWDDate Mod 10
1
Then, as this probably is ISO 8601 week numbering, the year is not the calendar year but the ISO 8601 year, which native VBA knows nothing about, thus a custom function is needed:
' First day of the week.
WeekStart = DateYearWeek(25, 2020, vbMonday)
' Requested day of week (which here is the same)
WeekDate = DateAdd("d", 1 - 1, WeekStart)
The function is not that convoluted:
' Returns the date of Monday for the ISO 8601 week of IsoYear and Week.
' Optionally, returns the date of any other weekday of that week.
'
' 2017-05-03. Gustav Brock, Cactus Data ApS, CPH.
'
Public Function DateYearWeek( _
ByVal IsoWeek As Integer, _
Optional ByVal IsoYear As Integer, _
Optional ByVal DayOfWeek As VbDayOfWeek = VbDayOfWeek.vbMonday) _
As Date
Dim WeekDate As Date
Dim ResultDate As Date
If IsoYear = 0 Then
IsoYear = Year(Date)
End If
' Validate parameters.
If Not IsWeekday(DayOfWeek) Then
' Don't accept invalid values for DayOfWeek.
Err.Raise DtError.dtInvalidProcedureCallOrArgument
Exit Function
End If
If Not IsWeek(IsoWeek, IsoYear) Then
' A valid week number must be passed.
Err.Raise DtError.dtInvalidProcedureCallOrArgument
Exit Function
End If
WeekDate = DateAdd(IntervalSetting(dtWeek), IsoWeek - 1, DateFirstWeekYear(IsoYear))
ResultDate = DateThisWeekPrimo(WeekDate, DayOfWeek)
DateYearWeek = ResultDate
End Function
but - as you can see - it calls some helper functions, which again call other functions, which will be too much to post here.
I can upload it somewhere, if you feel this will provide a solution for you.

getting Date months/ rest of days difference

is there a way to get these information from two dates:
calculate month between dates ( month for me is a complete month)
calculate rest of days between dates
here is my example:
start date:
01/01/2014
end date:
21/02/2014
i need a resualt like this : months:1 days:20
onother example:
start date:
15/01/2014
end date:
10/03/2014
i need a resualt like this : months:1 days:25
Using Java8 Date/Time API you may do it like so,
LocalDate startDate = LocalDate.of(2014, 1, 1);
LocalDate endDate = LocalDate.of(2014, 2, 21);
Period period = startDate.until(endDate);
System.out.println("months: " + period.getMonths() + " days: " + period.getDays());

Want to get previous date from current date

i have problem finding solution on getting previous date from current's date. but i only want the months to change. for example, now is August, i want the output to display 3 months ago = May.
Heres my code that i wrote
givenDate= "14-August-15"
DD = Day (givenDate)
'MsgBox DD
MM = Month (givenDate)
'MsgBox MM
YY = Year (givenDate)
'MsgBox YY
SysDate = DD&"/"&MM&"/"&YY
MsgBox Month(DateAdd("m", -3, "14-August-2015"))
'MsgBox(FormatDateTime(SysDate,1))
If you just need to display the name of the month that was 3 months ago, combine DateAdd(), Month(), and MonthName(). For example:
Dim dt1, dt2
dt1 = Date() ' Use today's date, for example
dt2 = DateAdd("m", -3, dt1) ' Subtract 3 months
WScript.Echo MonthName(Month(dt2)) ' Display the name of the month
Month() returns the month number (1 - 12). MonthName() takes that number and returns the name of the month ("January" - "December").

Which Months Fall Between Any Two Dates?

I get show months only on date interval, not days.
For i=initDate to endDate
response.write month(i)
next
in this case the result is: 5555555666666667777777......
Is repeat months for each day in interval, but i get display result only months.
For example: 5,6,7,8,9,10,11,12...
Think #John is on the right track with their answer.
But couldn't you simplify the approach by getting the start and end month from your initDate and endDate variables and stepping through the months?
Something like;
Dim initMonth, endMonth
initMonth = Month(initDate)
endMonth = Month(endDate)
For i = initMonth To endMonth
Response.Write i
Next
If you are confident that the initDate and endDate are always Date values you could simplify it further.
For i = Month(initDate) To Month(endDate)
Response.Write i
Next
I'm not sure I understood but maybe this will help. It stores the last month displayed in a variable (lastMonth) and only displays the month if it has changed. It shows the first month because lastMonth is set to zero when created and there should never be a month in the loop of zero.
Dim lastMonth
lastMonth = 0
For i=initDate to endDate
If lastMonth <> Month(i) Then
response.write month(i)
lastMonth = Month(i)
End If
next

Date after number of days in lua scripting

I am new to lua scripting. I have a startDate ("03-05-2014" as "dd-mm-yyyy") and a span in days (2) Can anyone help me how to get the endDate based on the startDate and span?.
Example startDate span endDate
--------- ---- -------
03-05-2014 2 05-05-2014
(dd-mm-yyyy) (dd-mm-2014)
You don't need to do any math here. os.time and os.date will do it for you.
local day, month, year = ("03-05-2014"):match("(%d%d)-(%d%d)-(%d%d%d%d)")
local span = 64
local endtime = os.time({day = day + span, month = month, year = year})
print(os.date("%c", endtime))
I'm not going to write the whole program for you, but here's something you can start with:
Get the day, month and year from the string:
local day, month, year = string.match('03-05-2014', '(%d%d)-(%d%d)-(%d%d%d%d)')
day, month, year = tonumber(day), tonumber(month), tonumber(year)
Use os.time to get the timestamp of a start time. You can
then add 3600 * 24 * 2 seconds (2 days) to get the timestamp of the end time.
Use os.date to formats the string from a timestamp.
This could help you
local dayValue, monthValue, yearValue = string.match('31-05-2014', '(%d%d)-(%d%d)-(%d%d%d%d)')
dayValue, monthValue, yearValue = tonumber(dayValue), tonumber(monthValue), tonumber(yearValue)
now = os.time{year = yearValue, month = monthValue, day = dayValue}
numberOfDays = now + 2 * 24 * 3600
print(os.date("%c",numberOfDays))
dateAfterNumberOfDays = os.date("%a %d %B %Y, %H%p%M",numberOfDays)
print ("\nafter number of days "..dateAfterNumberOfDays) -- give you date after number of days