I have a scheduled task that runs a powershell script, which runs an executable with some parameters. One of the parameters is the current week number.
I am using the following code in powershell to retrieve the weeknumber:
get-date -uformat %V
This seems to return an incorrect weeknumber
- 2014-05-04 (sunday), returns week nr 18 (as expected)
- 2014-05-05 (monday), returns week nr 18 (expected 19)
- 2014-05-06 (tuesday), returns week nr 19 (as expected)
In my registry the first day of week is set to Monday (HKEY_CURRENT_USER->Control Panel->International->iFirstDayOfWeek = 0)
Does anyone know where powershell retrieves it's firstDayOfWeek setting from? Should (/can) I otherwise set it in the script?
I know a workaround is to use .AddDays(1), but I'm looking for a neater solution.
Edit: Ok this is really weird, I'm receiving different 'first day of week's for different years..
2012 - On saturday:
PS C:\Users\A730978F> get-date -date "2012-01-08" -uFormat "%Y-%m-%d %a Week: %V"
2012-01-08 Sun Week: 2
PS C:\Users\A730978F> get-date -date "2012-01-09" -uFormat "%Y-%m-%d %a Week: %V"
2012-01-09 Mon Week: 2
PS C:\Users\A730978F> get-date -date "2012-01-10" -uFormat "%Y-%m-%d %a Week: %V"
2012-01-10 Tue Week: 2
PS C:\Users\A730978F> get-date -date "2012-01-11" -uFormat "%Y-%m-%d %a Week: %V"
2012-01-11 Wed Week: 2
PS C:\Users\A730978F> get-date -date "2012-01-12" -uFormat "%Y-%m-%d %a Week: %V"
2012-01-12 Thu Week: 2
PS C:\Users\A730978F> get-date -date "2012-01-13" -uFormat "%Y-%m-%d %a Week: %V"
2012-01-13 Fri Week: 2
PS C:\Users\A730978F> get-date -date "2012-01-14" -uFormat "%Y-%m-%d %a Week: %V"
2012-01-14 Sat Week: 3
2013 - On monday:
PS C:\Users\A730978F> get-date -date "2013-01-06" -uFormat "%Y-%m-%d %a Week: %V"
2013-01-06 Sun Week: 1
PS C:\Users\A730978F> get-date -date "2013-01-07" -uFormat "%Y-%m-%d %a Week: %V"
2013-01-07 Mon Week: 2
PS C:\Users\A730978F> get-date -date "2013-01-08" -uFormat "%Y-%m-%d %a Week: %V"
2013-01-08 Tue Week: 2
2014 - On Tuesday:
PS C:\Users\A730978F> get-date -date "2014-01-05" -uFormat "%Y-%m-%d %a Week: %V"
2014-01-05 Sun Week: 1
PS C:\Users\A730978F> get-date -date "2014-01-06" -uFormat "%Y-%m-%d %a Week: %V"
2014-01-06 Mon Week: 1
PS C:\Users\A730978F> get-date -date "2014-01-07" -uFormat "%Y-%m-%d %a Week: %V"
2014-01-07 Tue Week: 2
2015 - On wednesday:
PS C:\Users\A730978F> get-date -date "2015-01-04" -uFormat "%Y-%m-%d %a Week: %V"
2015-01-04 Sun Week: 1
PS C:\Users\A730978F> get-date -date "2015-01-05" -uFormat "%Y-%m-%d %a Week: %V"
2015-01-05 Mon Week: 1
PS C:\Users\A730978F> get-date -date "2015-01-06" -uFormat "%Y-%m-%d %a Week: %V"
2015-01-06 Tue Week: 1
PS C:\Users\A730978F> get-date -date "2015-01-07" -uFormat "%Y-%m-%d %a Week: %V"
2015-01-07 Wed Week: 2
(Note that I haven't tried all the weeks for every year)
Edit2:
After looking over the examples again, it seems the week number is based on the amount of '7 days' that have passed. I guess it doesn't actually use any 'first day of week' setting at all. I guess I'll have to make some calculation accordingly.
Thanks for the help anyway!