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
Related
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
I want to get the last Monday date for the given date. For example If my input is 190113 I want the output as 190107 which is last Monday.
if {$current_date == "Mon"} {
set startday [clock seconds]
set startday [clock format $startday -format %y%m%d]
puts $startday
} else {
puts "no monday today"
#I don't know how to get last monday date
}
This can be done fairly simply, by taking advantage of the fact that clock scan has quite a complex parser, and you can supply a timestamp that everything is relative to via the -base option. Also, both clock scan and clock format take -format options so that you can specify exactly what is going on in your input and output data.
proc getLastMonday {baseDate} {
set base [clock scan $baseDate -format "%y%m%d"]
set timestamp [clock scan "12:00 last monday" -base $base]
return [clock format $timestamp -format "%y%m%d"]
# This would work as a one-liner, provided you like long lines
}
Demonstrating:
puts [getLastMonday 190113]; # ==> 190107
puts [getLastMonday 190131]; # ==> 190128
Reference: https://www.tcl.tk/man/tcl/TclCmd/clock.htm#M22
Here's a sample code-snippet for the purpose. Added inline comments for understanding:
proc get_last_monday_date {date} {
# Get the end timestamp for the specified date
set end_timestamp [clock scan ${date}-23:59:59 -format %y%m%d-%H:%M:%S]
# Get day of the week for the current date
set day_of_week [clock format $end_timestamp -format %u]
# Sunday may report as 0 or 7. If 0, change to 7
# if {$day_of_week == 0} {
# set day_of_week 7
# }
# Monday is 1st day of the week. Monday = 1.
# Find how many days to go back in time
set delta_days [expr $day_of_week - 1]
# Multiply the delta by 24 hours and subtract from end of the day timestamp
# Get the timestamp for the result. That's last Monday's timestamp.
return [clock format [clock add $end_timestamp -[expr $delta_days * 24] hours] -format %D]
}
puts "Last Monday for 01-Jan-2019: [get_last_monday_date 190101]"
puts "Last Monday for 06-Jan-2019: [get_last_monday_date 190106]"
puts "Last Monday for 15-Jan-2019: [get_last_monday_date 190115]"
puts "Last Monday for 31-Jan-2019: [get_last_monday_date 190131]"
Execution output:
Last Monday for 01-Jan-2019: 12/31/2018
Last Monday for 06-Jan-2019: 12/31/2018
Last Monday for 15-Jan-2019: 01/14/2019
Last Monday for 31-Jan-2019: 01/28/2019
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.
So my default values for startDate and endDate in SSRS were set up with the following ssrs expressions.
first day of previous month ssrs expression
=DateAdd(DateInterval.Month, -1, DateSerial(Year(Date.Now), Month(Date.Now), 1))
last day of previous month ssrs expression
=DateAdd(DateInterval.Day, -1, DateSerial(Year(Date.Now), Month(Date.Now), 1))
But that won't work alone in my case unless I want to go in on the 16th of every month and generate this report for the people requesting it for the first 15 days of the current month.
So in my default value expression for start date i am trying this iif statement...
= iif(
DatePart(DateInterval.Day, Today() <> "20",
DateInterval.Month, -1, DateSerial(Year(Date.Now), Month(Date.Now), 1),
DateInterval.Month, 1, DateSerial(Year(Date.Now), Month(Date.Now), 1)
)
Not working out so well. So what i'm trying to do is.....
Change the default start and end date based on what day of the current month it is, So if current day of the current month equals 16, make start date 1 of current month and end date 15 of current month, if current day of the month isn’t 16 make start date first of previous month and end date last day of previous month. So then the only thing needed is to get subscription emails and what day to send them out on.
Untested, but what if you try this? (for your start date parameter):
= iif(
DatePart(DateInterval.Day, Today()) <> "16",
DateAdd(DateInterval.Month, -1, DateSerial(Year(Date.Now), Month(Date.Now), 1)),
DateSerial(Year(Date.Now), Month(Date.Now), 1)
)
I'm trying to find all Thursdays within a given Month using VBScript.
Can anyone help?
Here is one way;
base_date = cdate("21 aug 2011")
'get 1st thursday;
thurs = dateserial(year(base_date), month(base_date), 1)
if (weekday(thurs) <> 1) then thurs = 5 - weekday(thurs) + thurs
'loop subsequent;
do until month(thurs) <> month(base_date)
msgbox thurs
thurs = dateadd("d", 7, thurs)
loop
While the accepted answer does the job it's overly complicated for something that can be accomplished with WeekDay() function and a For loop.
Dim day
Dim startdate: startdate = CDate("21 aug 2011")
Dim enddate
'Get first day of month.
startdate = DateSerial(Year(startdate), Month(startdate), 1)
'Get last day of month.
enddate = DateAdd("m", 1, startdate) - 1
For day = startdate To enddate
If WeekDay(day) = vbThursday Then WScript.Echo day & " = " & WeekDayName(WeekDay(day))
Next
Output:
04/08/2011 = Thursday
11/08/2011 = Thursday
18/08/2011 = Thursday
25/08/2011 = Thursday
Any Date and Time constant can be used here to look for different or multiple weekdays with a little tinkering.