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)
Related
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));
}
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
How to count days between date range with a specific day?
Example:
START_DT = January 1, 2014;
END_DT = January 31, 2014;
Day = :SampleDay
Sample Result:
Monday = 4,
Tuesday = 4,
Wednesday = 5
Please help. :|
Are you looking for something like this,
WITH t(date1, date2) AS
(
SELECT to_date('01/01/2014', 'dd/mm/yyyy'),
to_date('31/01/2014','dd/mm/yyyy')+1 -- Adding 1 to calculate the last day too.
FROM DUAL
)
SELECT count(days) day_count, day
DAY
FROM(
SELECT date1 + LEVEL -1 days,
to_char(date1 + LEVEL -1, 'FmDay') DAY, --Use `FmDay`, this will remove the Embedded spaces.
to_char(date1 + LEVEL -1, 'D') DAY#
FROM t
CONNECT BY LEVEL <= date2 - date1
)
WHERE day = 'Monday' --Filter with day, if you want to get the count for a specific day.
GROUP BY DAY, day#
ORDER BY day#;
You wont have a direct solution to this. In oracle you have this form to know what day of the week is a specific date:
to_char(to_date('01012014', 'ddmmyyyy'), 'Day')
I would recommend to you to make a store procedure with a simple algorithm which receive that three parameters and then display the information you need. Put it in a query and it is done.
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.
I want to return ISO standard week numbers (ie week 1-52/53) from a date.
I have tried using the built in function strftime, but with no success.
Can anyone suggest a way without having to write a custom C or other function.
I know this is an old question, but recently I was looking for an efficient solution for the same problem, and this is what I came up with:
SELECT
my_date,
(strftime('%j', date(my_date, '-3 days', 'weekday 4')) - 1) / 7 + 1 as iso_week
FROM my_table;
The basic idea is to calculate the ISO week number by simply performing integer division of the day of year of the Thursday of the date being looked up (my_date) by 7, giving a result between 0 and 52, and then adding 1 to it. And the subtraction by 1 just before the division is there just for the alignment of the division: Thursday of week 1, for example, can have a day of year between 1 and 7, and we want the result of the division to be 0 in all cases, so we need to subtract 1 from the day of year before dividing it by 7.
What specifically did you try?
This works for me (using built-in SQLite from Python REPL):
import sqlite3
c = sqlite3.connect(':memory:')
c.execute('''create table t (c);''')
c.execute('''insert into t values ('2012-01-01');''')
c.execute('''select c, strftime('%W',c) from t;''').fetchone()
# -> (u'2012-01-01', u'00')
OK, so I managed to answer my own question. For anyone who might need a similar solution, this is what I came up with. Please note, I do not have an IT background and my SQL is self taught.
General process
Get the Thursday of the week the date belongs too.
Get the 4th of January of the year of that Thursday.
Get the Thursday of the week the 4th January date belongs too.
Subtract Step 2 with Step 3, divide by 7 and add 1.
Translated into SQLite....
SELECT
date,
CASE CAST (strftime('%w', wknumjanfourth) AS INTEGER)
WHEN 0 THEN ((JULIANDAY(datesThur) - JULIANDAY(strftime("%s", wknumjanfourth) - 259200, 'unixepoch')) / 7) + 1
ELSE ((JULIANDAY(datesThur) - JULIANDAY(DATE(strftime("%s", wknumjanfourth) - (86400 * (strftime('%w', wknumjanfourth) - 1)), 'unixepoch'), '+3 day')) / 7) + 1
END AS weeknum
FROM
(
SELECT
date,
datesThur,
DATE(datesThur,'start of year','+3 day') AS wknumjanfourth
FROM
(SELECT
date,
CASE CAST (strftime('%w', date) AS INTEGER)
WHEN 0 THEN DATE(strftime("%s", date) - 259200, 'unixepoch')
ELSE DATE(DATE(strftime("%s", date) - (86400 * (strftime('%w', date) - 1)), 'unixepoch'), '+3 day')
END AS datesThur
FROM TEST
))
If anyone can improve this SQL, I would appreciate the feedback.