Why when I use
Time.now.strftime('%Y%W')
or
Date.today.strftime('%Y%W')
they return 201912 while it should be 201913 as we are in week 13, not 12.
How to get the current week number?
If you look in the documentation, it says this:
Week number: The first week of YYYY that starts with a Sunday or
Monday (according to %U or %W). The days in the year before the first
week are in week 0.
%U - Week number of the year. The week starts with Sunday. (00..53)
%W - Week number of the year. The week starts with Monday. (00..53)
January 1st, 2019 was on a Tuesday, so that would have been week 0 - making today week 12.
Time.now.strftime('%Y%V')
Would give you the output your looking for.
%V - Week number of the week-based year (01..53)
You have to look for the week-based year.
Related
I am trying to figure out the Prometheus query for average CPU usage for current week and last week from ONLY Monday to Friday excluding the weekends. I have come up with a query which fetches the information from last 1 hour for current time and last hour a week ago which is as below
For current last 1 h:-
1 - avg(rate(node_cpu_seconds_total{mode="idle"}[1h])) by (instance)
For over last hour a week ago:-
1 - avg(rate(node_cpu_seconds_total{mode="idle"}[1h] offset 1w)) by (instance)
Did anyone had similar requirements for finding the same from Monday to Friday please?
How can I check whether an email was sent this week or not from the datetime parsed from the email? For today we can do:
yourdatetime.date() < datetime.today().date()
But for a week, first we need to define what a week is, which in our case is all emails since the previous Friday. I should be able to compute it by hand, but trying to see if there are datetime functions I can use to make the code more readable for the next person.
The best you could get directly would be the use of strftime to get the week number. Extract from the page on strftime() and strptime() Behavior :
%U : Week number of the year (Sunday as the first day of the week) as a zero padded decimal number. All days in a new year preceding the first Sunday are considered to be in week 0. Output : 00, 01, ..., 53
%W : Week number of the year (Monday as the first day of the week) as a decimal number. All days in a new year preceding the first Monday are considered to be in week 0. Output : 00, 01, ..., 53
But ... this only accept either monday or sunday as first day of the week, neither friday, nor saturday. If it is not enough, you will have to develop your own algorythm in a dedicated function.
Hi we have requirement to determine week of date by considering "Sunday to Saturday" as one week but i went through the link [oracle function][1]
[1]: http://www.techonthenet.com/oracle/functions/to_char.php here there are options like 'IW'(week of year ISO standard) which calculates week of year by considering Monday to Sunday as one week but we have specs to consider "Sunday to Saturday" as one week.Can any one suggest how to calculate ?
In order to determine the "week number" you need basically two informations:
On which day does your week start? This you provided, week begins on Sunday
Which week do you consider as first week of the year? This you did
not tell us.
The second definition may overrule the first one, e.g. "week 1 starts on the first day of the year and continues to the seventh day of the year." as used in Oracle TO_CHAR(..., 'WW')
i need a correct weekly ISO 8601 recurring time interval.
For example, repeat:
Each Monday, 19:00.
I already tried a lot of examples, but nothing worked correctly :-(
Thx for any help!
R/2014-W01-1T19:00:00/P1W
Represents a unbounded recurrence with a start date and a duration of one week. The recurrence starts at the first day (Monday) of the week number 01 in the week year 2014. I have used a week date, but you can substitute it with a calendar date or ordinal date, just make sure the date falls on a Monday.
One of the interview question..
If
1st january 2006 --> Sunday
Then
1st january 2010 --> ???
add 1 day for each non-leap year and 2 days for each leap year whatever comes that value mod 7.And finally add that amount of days with the given day.
for eg.
2007=1 day
2008=2 days
2009=1 day
2010=1 day
so total 5 days
and 5 % 7=5
Add 5 days in sunday,so you will get Friday
Year 2006 is not a leap year, it has 365 days.
Year 2007 has 365 days.
Year 2008 is a leap year, it has 366 days.
Year 2009, 365 days.
So there're 365+365+366+365==1461 days different of the two dates.
One week has 7 days, and 1461 mod 7 is 5, 5 days after Sunday is Friday.
So the answer is Friday.