I need some help. I want to get the week number of a specific date with the following system:
The week containing January 1st is the first week of the year. The week begins on Sunday. Range 1-53.
For example:
- Week 1 of 2015 begins on Sun 12/28/14 and ends on Sat 01/03/15.
- Week 1 of 2016 begins on Sun 12/27/15 and ends on Sat 01/02/15.
- Week 1 of 2017 begins on Sun 01/01/17 and ends on Sat 07/01/17.
I have read the WEEK(date[,mode]) function documentation, but none of these 'modes' match with my requirements.
How can I achieve this?
You can probably get this by using the DAYOFWEEK function. It will return you the value of what the day is (1-7 where 1 is Sunday, etc). From there, you can determine when the week has started and when it will end.
You have the WEEKOFYEAR as well. Please go thru manual
I was having the same problem, the function YEARWEEK solved it for me
SELECT YEARWEEK("2016-01-01");
>> 201552
I'm trying to get a Today vs This Day Last Year Comparison Report sorted.
I set a 'From' and 'To' date based on a 'Preset' parameter.
So if it is set to 'This Week' it sets the start date to Monday and the end date to Sunday.
What I want now is a Today vs This Day Last Year. So for example, if i were to do it for today, I would get 09/04/2015 vs 10/04/2014. So as today is Thursday, I want the nearest Thursday from 09/04/2014.
Also want the same for 'Yesterday'. So 08/04/2015 vs 09/04/2014
Is there a way to do this in either the Parameters SSRS expression, or within the custom 'Code' section?
To get this day last year, you can just subtract 52 weeks from today. Try this expression: =DateAdd("ww",-52,Today()). Likewise you can subtract 1 day from today to get yesterday: =DateAdd("d",-1,Today()).
I'm trying to create a query in google sheets that will pull data for the week so far. I can use the NDaysAgo filter to get the last 7 days but what I really need is from Monday to Sunday, and I don't want to have to go and edit the sheets every week. Also, if this can be done for weekly, I'd use the same formula for a month-to-date solution as well.
Thanks!
I am going to assume you are doing this in app Script.
Remember app script is just JavaScript. The following will return the previous Monday for a date.
var d = new Date();
var day = d.getDay(),
diff = d.getDate() - day + (day == 0 ? -6:1); // adjust if day is Sunday
Logger.log( new Date(d.setDate(diff)));
Use the getDay method of Date objects, you can find the number of the day of the week (being 0=Sunday, 1=Monday, etc).
You can then subtract that number of days plus one. result was:
[15-01-20 08:56:33:691 CET] Mon Jan 19 08:56:33 GMT+01:00 2015
How can I get the week day for 20110828 (28th of August 2011)?
I've tried this, but it didn't work:
var date:Date=new Date(2011,8,28);
//Also tried var date:Date=new Date(20110828);
trace(date.day);//0 - Sunday, 1 - Monday etc
Thank you.
I think you want the Date.getDay() method:
Returns the day of the week (0 for Sunday, 1 for Monday, and so on) specified by this Date according to local time. Local time is determined by the operating system on which the Flash runtimes are running.
The problem was that month 8 in AS is really month 9 in real life.
Examples:
'DD/MM/YYYY
"1/1/2009" should give `1`
"31/1/2009" should give `5`
"1/2/2009" should also give `5`
Format("1/2/2009", "ww") returns 6.
So, how can I get the correct result?
It's doing two things here which don't match your expectations, I think:
Assuming you want the week with Jan 1 in as week 1, and using Sunday as first day of the week
So it has week 1 running from Sunday 28th December 2008 to Saturday 3rd Jan 2009.
Week 6 would begin on Sunday 1st Feb by this method.
The ISO standard is for week 1 to be the one containing 4 days of January, or the first Thursday of the year (different ways of expressing the same thing).
You can specify this method of calculation and the first day of the week:
Format(SomeDate,"ww",vbMonday,vbFirstFourDays)
see here for syntax:
https://support.office.com/en-US/article/Format-Function-6F29D87B-8761-408D-81D3-63B9CD842530
Regardless of the day of the week your week starts on, you need to pass unambiguous date values. "31/1/2009" can only be one date (Jan 31st), but "1/2/2009" could be Jan. 2 (US style) or Feb. 1st (everybody else who has more sense that we USAns).
In this case, I'd use DateSerial() to make sure the date is not misinterpreted:
Format(DateSerial(2009,2,1), "ww", vbMonday)
While this is not causing your problem, because Access helpfully utilizes your system's localized date settings, I think it's something you should do anyway. You certainly are forced to do so in SQL in Access, so I don't think it's a bad habit in code and expressions.
This might work: Format(YourDate, "ww",vbMonday)
"Correct result" depends on the locale. Maybe VBA will let you pick a calendar-system, otherwise you're pretty much out of luck.
Note that First-Day-On-xxDay isn't your only problem. There is also variation on what a complete week is so Week 1 in one system could be Week 53 of the previous year in another system.
So test thoroughly and don't be seduced to "correct by 1".
There is a whole standard for week numbers: ISO-8601
http://en.wikipedia.org/wiki/ISO_8601#Week_dates
I had the same problem.
It showed week 53 and week 1, yet days in week 53 and week 1 are all in week 1
I first tried changing the date format in the Access Query to this:
OrderWeek: Format([OrderDate],"yyyy-ww",1,3) <-- But it did not do the trick.
You get dates like 2014-52 for week 52 and 2015-52 where it was week 1 before.
Also the sorting was not how I liked. It sorted the data as 2014-1, 2014-11, 2014-2 etc. I want it to show as 2014-01, 2014-02 .. 2014-11 etc.
So here is the new code to display both the year and the week correctly in an Access Query:
ActualWeek: IIf(DatePart("ww",[SomeDate])=53,DatePart("yyyy",[SomeDate])+1,DatePart("yyyy",[SomeDate])) & "-" & IIf(DatePart("ww",[SomeDate])=53,"01",IIf(DatePart("ww",[SomeDate])<10,"0" & DatePart("ww",[SomeDate]),DatePart("ww",[SomeDate])))
This now shows any days from week 53 as being part of week 1
If sunday is the first day of the week (as it is in some locales) then 6 is the correct weeknumber for "1/2/2009" (february 1. 2009)
In terms of the sorting, I had the same issue and used this code to resolve it:
IIf(Format([SomeDate],"ww")<10,Format([SomeDate],"yyyy-") & "0" & Format([SomeDate],"ww"),Format([SomeDate],"yyyy-ww"))
If the week number is less than 10, add a zero, else leave it as is.
Now the sorting is fine. Hope this helps somebody.