Powershell - Get date from week and weekday - powershell

I have a file containing 'year', 'week-number', and 'weekday', (example - '2022', '42', '4',) 42 represents week 42 of 2022, and the 4 represents tuesday. A 7 would represent sunday.
The first week is to be considered the first days until sunday, regardless if it is 7 days or not. Same principle for the last week. That week can also contain fewer than 7 days.
How can i translate this to a date like 'yyyyMMdd'?
Thanks

Note:
The following uses .NET's System.DayOfWeek enumeration to identify weekdays, which means that Sunday is 0, Monday is 1, ..., and Saturday is 6.
If your weekday-numbering scheme differs (which is what it sounds like), you'll have to map it onto the above.
# Convert the strings (representing CSV input) to numbers.
[int] $year, [int] $week, [int] $weekday = '2022', '42', '4'
# The day of the week you consider the start of a calendar week.
$startOfWeekDay = [DayOfWeek]::Monday
# Calculate the start of calendar week 1 for the given year:
# The Monday on or preceding Jan 1
# Note: This date may therefore fall into the previous year.
$Jan1 = [datetime]::new($year, 1, 1)
$StartOfWeek1 = $Jan1.AddDays(-((7 + $Jan1.DayOfWeek-$startOfWeekDay) % 7))
# Calculate the start of the target calendar week.
$StartOfTargetWeek = $StartOfWeek1.AddDays($week * 7)
# Calculate the target date by determining the desired weekday
# inside the target week.
$TargetDate = $StartOfTargetWeek.AddDays((7 + $weekday-$startOfWeekDay) % 7)
$TargetDateString = $TargetDate.ToString('yyyyMMdd')
# Output *for display* both the intermediate results and the final one.
[pscustomobject] #{
Jan1 = ($Jan1 | Out-String).Trim()
StartOfWeek1 = ($StartOfWeek1 | Out-String).Trim()
StartOfTargetWeek = ($StartOfTargetWeek | Out-String).Trim()
TargetWeekDay = [DayOfWeek] $weekday
TargetDate = ($TargetDate | Out-String).Trim()
TargetDateString = $TargetDateString
} | Format-List
Output:
Jan1 : Saturday, January 1, 2022 12:00:00 AM
StartOfWeek1 : Monday, December 27, 2021 12:00:00 AM
StartOfTargetWeek : Monday, October 17, 2022 12:00:00 AM
TargetWeekDay : Thursday
TargetDate : Thursday, October 20, 2022 12:00:00 AM
TargetDateString : 20221020

Related

lastquarter, previousquarter and prioryear - PowerShell Functions

I am running below to get
#lastquarter - SELECT DATEADD(dd, -1, DATEADD(qq, DATEDIFF(qq, 0, GETDATE()), 0)), 23 - Returns 2021-12-31 00:00:00.000
#previousquarter - SELECT DATEADD(qq, -1,DATEADD(dd, -1, DATEADD(qq, DATEDIFF(qq, 0, GETDATE()), 0))), 23 - Returns 2022-12-31 00:00:00.000
#prioryear - SELECT DATEADD(yy, -1,DATEADD(dd, -1, DATEADD(qq, DATEDIFF(qq, 0, GETDATE()), 0))), 23; - Returns 2022-09-30 00:00:00.000
How would I write this in Powershell to get above?
Does it have any functions I can use. I looked into get-date but it seemes getting above in PowerShell is not as straightforward
Would appreciate some assistance with building up the logic in PS
Here's some expressions that give the values you're after. I've not tested them extensively, so you might want to write some Pester tests or something to validate them with edge cases, etc...
It also assumes you don't care about the time part - you might need to clear that in the $quarterStart expression if that matters...
$timestamp = [datetime] "2022-01-12";
write-host "timestamp = $timestamp";
# timestamp = 01/12/2022 00:00:00
# find the start of the *current* quarter - it's easier and
# clearer to work out the others from this interim value
$quarterStart = $timestamp.AddDays(-$timestamp.Day + 1).AddMonths(-($timestamp.Month-1) % 3);
write-host "quarter start = $quarterStart";
# quarter start = 01/01/2022 00:00:00
# end of the quarter before the current one
# (i.e. one day before the current quarter start)
$lastQuarter = $quarterStart.AddDays(-1);
write-host "last quarter = $lastQuarter";
# last quarter = 12/31/2021 00:00:00
# end of the quarter before the last one
# (i.e. three months and one day before the current quarter start)
$previousQuarter = $quarterStart.AddMonths(-3).AddDays(-1);
write-host "previous quarter = $previousQuarter";
# previous quarter = 09/30/2021 00:00:00
# end of the previous year
# (i.e. 31st December of the previous year)
$lastYear = new-object DateTime(($timestamp.Year - 1), 12, 31);
write-host "last year = $lastYear";
# last year = 12/31/2021 00:00:00

How to plot a graph for specific day of every month for whole Year MATLAB

I have two variables called x and y. Each has 24*365 values. 24 presents the number of hours in a day and 365 presents the number of days in a year. I am plotting the values of 12th hour and 25th day by the following command:
plot(x(12:12,25), y(12:12,25))
Now I want to do the same for every 25th day for a whole year. Like 25th of Jan, 25th of Feb, 25th of March. I am not bothered about values of hours but I don't know how to create its logic as every month has different number of days.
You can generate the day of year number by getting the datenum values for the 25th of each month and subtracting the datenum of the 1st Jan that year.
dayIdx = datenum(2022,1:12,25) - datenum(2022,1,1) + 1;
Then just use this as your column index
plot(x(12,dayIdx), y(12,dayIdx))
The choice of 2022 above is arbitrary, as long as you pick a non-leapyear to get the 365-day year correct.
The datetime data type is awesome for this type of work.
%Make a vector of datetimes
ts = ( datetime(2001,1,1,0,0,0):hours(1):datetime(2001,12,31,023,0,0) )';
%Find the datetimes which are on the 25th day of the month, and the 12th
%hour of the day
mask = (day(ts) == 25) & (hour(ts) == 12) ;
%Confirm
ts(mask)
The result is below.
(You likely want to use the mask variable itself for your task. Sometimes you want to use find on the logical statement to get a list of indexes instead.)
ans =
12×1 datetime array
25-Jan-2001 12:00:00
25-Feb-2001 12:00:00
25-Mar-2001 12:00:00
25-Apr-2001 12:00:00
25-May-2001 12:00:00
25-Jun-2001 12:00:00
25-Jul-2001 12:00:00
25-Aug-2001 12:00:00
25-Sep-2001 12:00:00
25-Oct-2001 12:00:00
25-Nov-2001 12:00:00
25-Dec-2001 12:00:00

Given an ISO 8601 week number, get date of first day of that week in LibreOffice Calc spreadsheet

LibreOffice Calc spreadsheet offers a function ISOWEEKNUM to return the standard ISO 8601 week number of the specified date.
I want the opposite.
➠ Given a standard week number, give me the date of the first day of that week (the Monday date).
Passing integers is acceptable. Also nice if able to pass a string in standard format.
Like this:
DATE_OF_ISOWEEKNUM( 2017 , 42 ) ➝ date of Monday of week 42 in week-based year 2017
DATE_OF_ISOWEEKNUM( "2017-W42" ) ➝ date of Monday of week 42 in week-based year 2017
Ideally, I would be able to pass a number 1-7 for Monday-Sunday to specify the day-of-week for which I want a date. Something like this:
DATE_OF_ISOWEEKNUM( 2017 , 42 , 1 ) ➝ date of Monday of week 42 in week-based year 2017
DATE_OF_ISOWEEKNUM( "2017-W42-1" ) ➝ date of Monday of week 42 in week-based year 2017
DATE_OF_ISOWEEKNUM( 2017 , 42 , 7 ) ➝ as above, but Sunday
DATE_OF_ISOWEEKNUM( "2017-W42-7" ) ➝ as above, but Sunday
Example:
Formula:
=DATE(B$1,1,$A4*7)+(2-WEEKDAY(DATE(B$1,1,$A4*7)))-7*(ISOWEEKNUM(DATE(B$1,1,1))=1)
Calculate the date of day (weeknumber * 7) in the year.
Correct the day to be weekday Monday.
Correct to 7 days before, if the first day of the year is in the
first ISO weeknumber.

how to compare two custom dates in powershell v2.0

Is it possible to compare 2 custom dates. Am trying check if variables hold date1 is lessthan date2, if so, report saying date1 is older date.
I getting both dates from a. date1 from log file and date2 from application itself
now, both date1 and date2 are in required format ie,
$Date1 = Tue,Aug 16, 2016 12:40:03
$Date2 = Mon,Aug 22, 2016 16:33:02
my next step is compare these 2 dates and report if date1 is older date compare to Date2, which I don't know how to proceed.. Any help/ideas is much appreciated.
Thanks to Pete and Ansgar Wiechers
updated working Code :
$Date1DateTime = [DateTime]::ParseExact($Date1,'ddd,MMM d, yyyy, HH:mm:ss',[Globalization.CultureInfo]::InvariantCulture); $Date2DateTime = [DateTime]::ParseExact($Date2,'ddd,MMM d, yyyy, HH:mm:ss',[Globalization.CultureInfo]::InvariantCulture); $Date1DateTime -lt $Date2DateTime
You can only compare date strings if the string sort order is the same as the date sort order. For instance, date strings in ISO format are comparable:
2016-08-16T12:40:03
2016-08-22T16:33:02
Date strings in your custom format are not, because T comes after M, but August 16 should actually come before August 22:
Tue,Aug 16, 2016 12:40:03
Mon,Aug 22, 2016 16:33:02
If you don't have the date strings in ISO format it's usually better to parse them into actual DateTime values (as #PetSerAl suggested), particularly if your reference value is originally a DateTime anyway.
$fmt = 'ddd,MMM d, yyyy, HH:mm:ss'
$culture = [Globalization.CultureInfo]::InvariantCulture
$Date1 = Get-Date $LogFileDate
$val = (b2b.exe -readparams $param | Select-Object -Skip 1 -First 1) -split '='
$Date2 = [DateTime]::ParseExact($val[1], $fmt, $culture)
if ($Date1 -lt $Date2) {
...
}

Custom Function - Calculate date of first date of next quarter

I am trying to create a custom function of given a date in Filemaker, I would like it to determine if this week of fiscal year number is within the first week of quarter else it will calculate the first date next quarter. Our Fiscal year starts on July 1
So for defined requirements our FY starts on July 1 and Qtrs are on week # 1, 14, 27, 40 our weeks go from 1-52 and the week starts on Tuesday (Defined as day 3). If FY starts on Monday than the first week will be from Mon-Tues (Therefore a shortweek) then week 2 will be a full 7 day week.
Example---> If I have a date 09/09/2011 that would be week 11 in Q1, therefore since it is not the first week of the quarter I would like the following date for the next Qtr which would be Wk 14 first date of 9/27/2011. So my evaluation needs to determine whether the given date is within the first week of a qtr (weeks 1, 14, 27, 40) or provide the first week of the next qtr.
Also here is the initial CF I was working with from Brian Dunnings site.
https://www.briandunning.com/cf/147
I know this would be developed in filemaker but there maybe something developed in another language which may apply...
Thanks in advance
Try this as your starting point:
Let ( [
startFY = Date ( 7 ; 1 ; Year ( Datefield ) - ( Month ( Datefield ) < 7 ) ) ;
firstTuesday = startFY - Mod ( startFY - 2 ; 7 ) ;
fiscalWeek = Div ( Datefield - firstTuesday ; 7 ) ; //numbering starts at 0
targetWeek = 13 * Ceiling ( fiscalWeek / 13 )
] ;
firstTuesday + 7 * targetWeek
)
Note that the result is always a Tuesday; you may want to adjust this for the boundary cases of fiscal year start and end. The way it works now, you'll get a result of July 30, 2015 for both June 15, 2015 and July 6, 2015.