ASP Classic Get Next Date for a WeekDayName - asp-classic

I would like to specify a number that specifies the day of week and then have ASP get the upcoming date for that week day specified.
Example:
Dim xWeekDay
xWeekDay=1 ' <-- 1 would be a Monday...and Sunday would be 7
Dim NextDdate
NextDdate= ???? <-- I want to calculate and show the Upcoming Date here
So the above line would look like this when it's populated.
NextDdate=7/1/2013

Try this:
today = Weekday(Date, vbMonday)
If xWeekDay > today Then
NextDate = Date + (xWeekDay - today)
Else
NextDate = Date + (xWeekDay + 7 - today)
End If
Weekday(Date, vbMonday) is the number of the currend day of the week (with Monday being set as the first weekday). If xWeekDay is in the future (xWeekDay > today), then the next occurrence is xWeekDay - today days away. Otherwise it's xWeekDay + 7 - today days away. Add that difference to the current date and you have the date you're looking for.

Related

How to set start of the week as Sunday and end as Saturday in Dart?

I can get start of the week as Monday using method below:
DateTime findFirstDateOfTheWeek(DateTime date) {
return date.subtract(Duration(days: date.weekday - 1));
}
and end of the week, the Sunday, as:
DateTime findLastDateOfTheWeek(DateTime date) {
return date.add(Duration(days: DateTime.daysPerWeek - date.weekday));
}
Now the requirements are, to treat Sunday as week start and Saturday as week end. I tried tweaking above method but could not get it working. Any help would be appreciated.
Thanks
Since DateTime.weekday counts from 1 (Monday) to 7 (Sunday):
date.subtract(Duration(days: date.weekday - 1))
will give us the date of the start of the week using what DateTime considers to be the first day of the week (Monday). To treat Sunday as the first day of the week instead, we can try to get the previous day from that:
date.subtract(Duration(days: date.weekday - 1)).subtract(const Duration(days: 1))
The above is algebraically equivalent to date - (weekday - 1) - 1, which can be simplified to just date - weekday. But that's not quite right because weekday ranges from [1, 7], and we never want to subtract 7 days. In that case, we want to subtract 0. The modulus operator handles that nicely, ultimately giving us:
DateTime findFirstDateOfTheWeek(DateTime date) {
return date.subtract(Duration(days: date.weekday % DateTime.daysPerWeek));
}
To get the last day of the week where Saturday is treated as the last day of the week, you can just get the first day of the week and add 6 days:
DateTime findLastDateOfTheWeek(DateTime date) {
return findFirstDateOfTheWeek(date).add(
const Duration(days: DateTime.daysPerWeek - 1));
}

Weekly Period Column For Each Month in Power BI

I have a date table with a date column. I would like to have a weekly period for each month. So far, I have built a custom column such that each period starts on Monday and ends on Sunday:
let
startOfWeek = Date.StartOfWeek([Date]),
endOfWeek = Date.EndOfWeek([Date]),
yearOfStartOfWeek = Date.Year(startOfWeek),
yearOfEndOfWeek = Date.Year(endOfWeek),
valueToReturn = if yearOfStartOfWeek = yearOfEndOfWeek then
Text.From(Date.StartOfWeek([Date])) & " - " & Text.From(Date.EndOfWeek([Date]))
else
Text.From(Date.StartOfWeek([Date])) & " - " & Text.From(Date.EndOfWeek(endOfWeek))
in valueToReturn
This will give me the custom column "Weekly Period" below:
But what I need to have is this way of weekly period:
The first week for each month starts with first day of the month and that has to be a working day (not weekend). For example the first week of Sep 2019 starts with Monday 2nd . Also, the last weekly period for each month ends with the last day of month.
Do you know how to change this code or which other codes I need to use to have This weekly period like the column below? Many Thanks.
Also I want another column that gives me the week numbers of the months, as above. I would be grateful if someone can help me build this two custom columns in the date table.
I did it in the query designer (language M) with the following expressions. Go to Add Column > Custom Column (I did it in two separate columns):
'Get first day of month
FirstDayOfMonth = Date.StartOfMonth([Column1]) 'Make sure the outcome is a date format
'Get first working day of month (without Saturday and Sunday)
FirstWorkingDayOfMonth = if Date.DayOfWeek([FirstDayInMonth]) = Day.Saturday
then Date.AddDays([FirstDayInMonth], 2)
else if Date.DayOfWeek([FirstDayInMonth]) = Day.Sunday
then Date.AddDays([FirstDayInMonth], 1)
else [FirstDayInMonth]
For the week number column you can use the Date.DayOfWeek() function, like I used above.
For those that might be helpful, here I found the answer for Weekly Period:
let
startOfWeek = Date.StartOfWeek([Date]),
endOfWeek = Date.EndOfWeek([Date]),
weekOfStartOfWeek = Date.WeekOfYear(startOfWeek),
weekOfEndOfWeek = Date.WeekOfYear(endOfWeek),
valueToReturn = if (Date.Month(Date.StartOfWeek([Date])) = Date.Month([Date]) and Date.Month(Date.EndOfWeek([Date])) = Date.Month([Date]))
then
Text.From(Date.StartOfWeek([Date])) & " - " & Text.From(Date.EndOfWeek([Date]))
else if Date.Month(Date.StartOfWeek([Date])) <> Date.Month([Date]) then
Text.From([Date.StartOfMonth([Date])]) & " - " & Text.From(Date.EndOfWeek(endOfWeek))
else
Text.From(Date.StartOfWeek([Date])) & " - " & Text.From(Date.EndOfMonth([Date]))
in
valueToReturn

ASP Classic - Find last weeks Friday or any day using days as numbers (1-7, 1=Monday and so on..)

I would like to find last weeks Friday for example.
Using days as numbers (1 through 7) for example:
1= Monday and so on..
It would be something like this but I'm stuck at the GetLastWeek, Please see below, THANKS.
<%
dim weeknum
weeknum=5
dim GetLastWeek
GetLastWeek=???? <== FIND LAST WEEKS FRIDAY AS A DATE Eg: MM/DD/YYYY
%>
Example: Last weeks Friday was on: <%=GetLastWeek%>
I probably start by working out what is the current day of the week and working back from there, you can use something like this;
Dim today, offsetdays, lastfri
'WeekDay() returns 1 - 7 (Sunday - Saturday).
today = WeekDay(Date())
'Workout the offset then use DateAdd() to minus that number of days.
Select Case today
Case 1 'Sunday
offsetdays = 2
Case 2 'Monday
offsetdays = 3
Case 3 'Tuesday
offsetdays = 4
Case 4 'Wednesday
offsetdays = 5
Case 5 'Thursday
offsetdays = 6
Case 6 'Friday
offsetdays = 7
Case 7 'Saturday
offsetdays = 1
End Select
lastfri = DateAdd("d", -offsetdays, Date())
Bear in mind this is pseudo coded (untested) and could probably be made better by storing the offsets in an array and using that to power the DateAdd() instead.
You can use the Weekday() function to find what day of the week any particular date is. With this you should be able to calculate anything else you like. There is a full reference for the function here:
http://www.w3schools.com/vbscript/func_weekday.asp

Given a date how to get Sunday & Saturday of that week

I want to get the Sunday & Saturday of the week from which a date is provided.
I have access to the following functions only:
getDate() returns a number from 0-6 (0 being sunday)
getDay() returns a number from 1-31
getMonth() returns a number from 0-11
getFullYear() returns the current year
I am doing this on titanium.
Per your description above, I came up with:
var sat = new Date(input.getFullYear(), input.getMonth(), 6 - input.getDate() + getDay());
var sun = new Date(input.getFullYear(), input.getMonth(), getDay() + (input.getDate() - 6));
If I follow the MDN doc, I come up with (works in Ti too):
var sat = new Date(input.getFullYear(), input.getMonth(), 6 - input.getDay() + input.getDate());
var sun = new Date(input.getFullYear(), input.getMonth(), input.getDate() + (input.getDay() - 6));
Where input is a javascript Date object.
The date object will take care or changing the month and/or year if necessary.
Hope this helps.

Find This Weeks Monday

How do I get the date of this current week's Monday. Where Monday is the first day of the week. So if I was to look up this weeks it would return the date 1/16/2012
I am using VBScript and ASP
Many thanks in advance..
Paul
Effectively, the Weekday function returns Sunday=1, Monday=2, etc. To get the Monday of the same week, you want to subtract:
Sunday (1): 6 days
Monday (2): 0 days
Tuesday(3): 1 day
...
Saturday(7): 5 days.
Or
Days to subtract = (Weekday - 2 + 7) Mod 7
So if d is a date, the Monday of the same week can be written as:
mondayofsameweek = DateAdd("d", -((Weekday(d) + 7 - 2) Mod 7), d)
VBScript has a function called WeekDay, it return 1 - 7, not sure whether 1 is Monday though, usually you can twiddle with that.
Either way get the weekday Thursday = 4? , so then you just need to take three days off your date with thae DateAdd function
In VBScript WeekDay returns the day of the week, starting with Sunday=1 (VBScript can be quirky like that). So just subtract two (Monday=2) from that value and call DateAdd.
monday = DateAdd("d",(WeekDay(Date())-2),Date())
I know the topic is a bit old, but I figured I'd share my working solution that I think is a bit more concise.
Where dDate is your current date:
dDate = DateAdd("d", -(WeekDay(dDate, vbMonday) - 1), dDate)
The second parameters of WeekDay is the day you want the week to 'start'. For me, in the US, WeekDay corresponds the default second parameter to vbSunday. Specifying vbMonday, we just get the difference (base 1) between our current weekday and Monday.
Subtract 1 and add the inverse to your current date should obtain your result.
Example:
WeekDay(2018-03-20, vbMonday) = -((2) - 1) = DateAdd("d", -1, 2018-03-20)

Resources