Week Number with Get-Date - powershell

I have this powershell command:
Get-Date -UFormat %V
On Monday it shows bad week number and I don't know why. How do I add one to get the correct week number? I set everywhere that first day of week is Monday but didn't help. Thanks.

Try using this method to get the week number of the year. Found here
function Get-WeekNumber([datetime]$DateTime = (Get-Date)) {
$cultureInfo = [System.Globalization.CultureInfo]::CurrentCulture
$cultureInfo.Calendar.GetWeekOfYear($DateTime,$cultureInfo.DateTimeFormat.CalendarWeekRule,$cultureInfo.DateTimeFormat.FirstDayOfWeek)
}
Get-WeekNumber # -> 7, for instance

What you are after is the ISO8601 weeknumber, where
a week starts at Monday
week 1 is the first week that contains a Thursday
To get this weeknumber, you can use the function below:
function Get-Iso8601WeekNumber([datetime]$DateTime = (Get-Date)) {
# ISO8601: Week starts on Monday and Week 1 is the week that contains Thursday
$calendar = [CultureInfo]::InvariantCulture.Calendar
$dow = $calendar.GetDayOfWeek($DateTime)
if ($dow -ge [DayOfWeek]::Monday -and $dow -le [DayOfWeek]::Wednesday) {
$DateTime = $DateTime.AddDays(3)
}
return $calendar.GetWeekOfYear($DateTime, [Globalization.CalendarWeekRule]::FirstFourDayWeek, [DayOfWeek]::Monday)
}
Using your example
Get-Iso8601WeekNumber "2020-02-17"
returns 8
Note the use of InvariantCulture as opposed to CurrentCulture

Related

How to get all dates of the week based on week number using powershell?

I'm using powershell 7.x and want to create an array of dates in a week based on the weeknumber. I'm able to generate the array and find the whole week dates but how can I get the date values returned in the output?
I have used [System.Globalization.ISOWeek]::ToDateTime([Int32] year,[Int32] week number,[DayOfWeek]:Monday), to fetch the days of the week. Is there a better and optimal way to fetch all the dates in the array based on the week number?
Here's the code below:
function fetchWeekDates {
$output = #{ Monday =[System.Globalization.ISOWeek]::ToDateTime(2022,1,[DayOfWeek]::Monday)}
$output += #{ Tuesday=[System.Globalization.ISOWeek]::ToDateTime(2022,1,[DayOfWeek]::Tuesday)}
$output += #{ Wednesday =[System.Globalization.ISOWeek]::ToDateTime(2022,1,[DayOfWeek]::Wednesday)}
.
.
.
$output += #{ Sunday =[System.Globalization.ISOWeek]::ToDateTime(2022,1,[DayOfWeek]::Sunday)}
return $output
}
Expected output:
Output
-------
01-03-2022
01-04-2022
01-05-2022
01-06-2022
01-07-2022
01-08-2022
01-09-2022
The following function accepts a week number, and optionally a year (defaults to the current) and the day of the week that is considered the start of a week (defaults to Monday), and outputs [datetime] instances representing all days in that week.
function Get-DatesInWeekNumber {
param(
[Parameter(Mandatory)]
[int] $WeekNumber,
[int] $Year = (Get-Date).Year,
[DayOfWeek] $FirstDayOfWeek = 'Monday'
)
$date = [Globalization.ISOWeek]::ToDateTime($Year, $WeekNumber, $FirstDayOfWeek)
0..6 | ForEach-Object { $date.AddDays($_) }
}
Note the use of the [datetime] type's .AddDays() method.
Sample call, which formats the [datetime] instances as desired:
Get-DatesInWeekNumber -WeekNumber 1 -Year 2022 |
ForEach-Object ToString MM-dd-yyyy
This produces the output as shown in your question.

Powershell how to check if friday is the last day of the month , returning True from a Function

I have tried to make a function in Powershell that produces a True if the last day of the month is friday.
function Get-LastFridayOfMonth([DateTime] $d) {
$lastDay = new-object DateTime($d.Year, $d.Month,[DateTime]::DaysInMonth($d.Year, $d.Month))
$diff = ([int] [DayOfWeek]::Friday) - ([int] $lastDay.DayOfWeek)
if ($diff -gT 0) {
return $lastDay.AddDays(- (7-$diff))
} else {
return $lastDay.AddDays($diff)
}
}
$testdate = Get-LastFridayOfMonth (Get-Date)
But how to check if that friday is the last day of the month ?
#Theo's answer reframes your question and gives a nice all-in-one solution, but to address your specific question:
"how to check if [a date] is the last day of the month ?"
you can just add one day to the date and see if the month changes. If it does change then the original date must be the last one in the month…
$testdate = [datetime] "2023-01-31"
$isLastDayOfMonth = $testdate.AddDays(1).Month -ne $testdate.Month
$isLastDayOfMonth
# True
$testdate = [datetime] "2023-01-30"
$isLastDayOfMonth = $testdate.AddDays(1).Month -ne $testdate.Month
$isLastDayOfMonth
# False
$testdate = Get-LastFridayOfMonth (Get-Date)
$isLastDayOfMonth = $testdate.AddDays(1).Month -ne $testdate.Month
$isLastDayOfMonth
# depends on if the month ends on a friday
I would change that function to just return the last days weekday so after calling it you can decide what weekday you get for the given date.
function Get-LastWeekDayOfMonth {
param (
[datetime]$Date = (Get-Date)
)
# .AddDays(-$Date.Day+1) sets the given date to the 1st of that month
$Date.AddDays(-$Date.Day+1).AddMonths(1).AddDays(-1).DayOfWeek
}
switch (Get-LastWeekDayOfMonth) {
'Friday' { "It's a Friday !" }
default { $_ }
}
Using the current date (February 2023) it would yield Tuesday
If you specify for instance March 2023
$d = Get-Date -Year 2023 -Month 3 -Day 1
switch (Get-LastWeekDayOfMonth -Date $d) {
'Friday' { "It's a Friday !" }
default { $_ }
}
The outcome will be It's a Friday !
If however you want a function to simply return $true or $false when the month end in a Friday or not, do this instead:
function Test-LastDayOfMonthIsFriday {
param (
[datetime]$Date = (Get-Date)
)
# .AddDays(-$Date.Day+1) sets the given date to the 1st of that month
$Date.AddDays(-$Date.Day+1).AddMonths(1).AddDays(-1).DayOfWeek -eq 'Friday'
}
Test-LastDayOfMonthIsFriday # --> $false
Test-LastDayOfMonthIsFriday -Date (Get-Date -Year 2023 -Month 3 -Day 1) # --> $true
Special thanks to mklement0 for the $Date.AddDays(-$Date.Day+1) part which always sets the date to the first day of that month

How can I check if today is the first Tuesday of the current quarter?

I'm trying to find in Powershell if today's date is greater than the first Tuesday of the quarter or if it is before the first Tuesday of the quarter.
The reason is my script should only make a change if today is past the first Tuesday of the quarter!
I know how to do it for the first Tuesday of a month, but really struggling to get it to work with quarters, whats the best way to do this?
# Get today's date (without a time-of-day component).
$today = (Get-Date).Date
$afterFirstTueInQuarter =
if ($today.Day -ge 7 -or $today.Month -notin 1, 4, 7, 10) { $true }
else { # in the first 6 days of the start of a quarter
# Find the first day of the month...
$firstDayOfMonth = Get-Date -Year $today.Year -Month $today.Month -Day 1
# ... and the month's first Tuesday.
$firstTue = $firstDayOfMonth.AddDays(([DayOfWeek] 'Tuesday' - $firstDayOfMonth.DayOfWeek + 7) % 7)
# Compare to today.
$today -gt $firstTue
}

Get date of a day in last week of a month using powershell

How to retrieve the date of the last Wednesday in the last week of month using powershell?
A quick way to get the date of, e.g. the last Wednesday of the current month could be
# Day to match
$day = "Wednesday"
# First, fetch the last day's date of the current month.
# I.e. add one month to the current date and remove today's date from the date.
# 3.4.2018 + 1 month - 3 days = 30.4.2018
$lastDay = (Get-Date).AddMonths(1).AddDays(-$(Get-Date).AddMonths(1).Day)
# Next remove one weeks worth of days, one day at a time,
# from $lastDay until the DayOfWeek is equals to Wednesday
# Finally, do something, e.g. write the date to the console.
1..7 | %{ if($lastDay.AddDays(-$_).DayOfWeek -eq $day) { `
Write-Host $lastDay.AddDays(-$_) `
}}

powershell to calculate 20th business day from today

trying to calculate\determine 6th Business from today in powershell.
is there a script\function available for same?
example :
if today's date is 4th Feb 2018, 6th business day from today is 12th feb 2018.
how do i get that date using powershell?
I think this would work for your situation.
It adds 2 days if the current iteration lands on a Saturday and 1 day if it lands on a Sunday. There may be a better way but I know this works.
Edit: I didn't think about it if you need to account for holidays let me know and I may be able to edit this script to include it.
$daystoadd = 7
$CurrentDate = Get-date
For ($i = 1; $i -le $daystoadd; $i++) {
$CurrentDate = $CurrentDate.adddays(1)
if ($CurrentDate.DayOfWeek -eq 'Saturday') {
$CurrentDate = $CurrentDate.adddays(2)
}
if ($CurrentDate.DayOfWeek -eq 'Sunday') {
$CurrentDate = $CurrentDate.adddays(1)
}
}
$CurrentDate
#Sumanth specifically asks for the sixth business day, so this should do the trick:
function SixthBusinessDay($date)
{
$daysToAdd = 8,8,8,8,8,10,9
$weekDay = $date.DayOfWeek.value__
$sixtBusinessDay = $date.AddDays($daysToAdd[$weekDay])
return $sixtBusinessDay
}
It's based on that all days require at least six days and one weekend to make it the sixth business day, but Friday requires two extra days and Saturday one extra day.
Running this:
(SixthBusinessDay (Get-date "2018-1-29")).ToString("dddd dd/MM")
(SixthBusinessDay (Get-date "2018-1-30")).ToString("dddd dd/MM")
(SixthBusinessDay (Get-date "2018-1-31")).ToString("dddd dd/MM")
(SixthBusinessDay (Get-date "2018-2-1")).ToString("dddd dd/MM")
(SixthBusinessDay (Get-date "2018-2-2")).ToString("dddd dd/MM")
(SixthBusinessDay (Get-date "2018-2-3")).ToString("dddd dd/MM")
(SixthBusinessDay (Get-date "2018-2-4")).ToString("dddd dd/MM")
gives the following result:
Tuesday 06-02
Wednesday 07-02
Thursday 08-02
Friday 09-02
Monday 12-02
Monday 12-02
Monday 12-02