How to declare an array using get-date? - powershell

I'd like to declare an array of dates in powershell, it's not a continuous range, so I cant do anything clever with loops.
I've tried using the array notation with a call to get-date, as below, but this returns an error
$logDateList =
#(Get-Date -Year 2017 -Month 08 -Day 22 -Hour 13
,Get-Date -Year 2017 -Month 08 -Day 20 -Hour 22)
How can I declare an array of dates in powershell?

Just wrap each Get-Date in brackets:
$logDateList = #((Get-Date -Year 2017 -Month 08 -Day 22 -Hour 13),(Get-Date -Year 2017 -Month 08 -Day 20 -Hour 22))

I'd argue that you should still use a loop for this to save yourself some code duplication:
$logDateList = '20 Aug 2017 22:00','22 Aug 2017 22:13' | ForEach-Object { Get-Date $_ }
Example using text for months in the interests of avoiding regional issues but you could format your dates in whatever way you like that works for you.

You can cast a string to a datetime like this:
[datetime]'2017-08-22 13:00'
and make your array
$logDateList = [datetime]'2017-08-22 13:00', [datetime]'2017-08-20 22:00'
and if you're doing that, make it one array of string dates, and cast to an array of datetime
[datetime[]] $logDateList = #(
'2017-08-22 13:00',
'2017-08-20 22:00'
)
I put them in ISO standard yyyy-MM-dd hh:mm order to avoid US/UK parsing confusion.

Related

How to Convert datetime formats using powershell

I Have a variable stored with this value
PS C:\Users\> $Time
Monday, November 30, 2020 8:55:01 AM
Sunday, October 18, 2020 11:10:01 PM
Sunday, November 8, 2020 10:40:34 PM
Sunday, November 8, 2020 11:47:37 PM
Sunday, November 8, 2020 10:59:57 PM
Tuesday, December 1, 2020 3:15:42 AM
Monday, November 30, 2020 7:00:32 PM
Monday, November 30, 2020 12:19:06 AM
Monday, November 30, 2020 7:01:34 PM
Tuesday, December 1, 2020 1:12:10 AM
Tuesday, December 1, 2020 2:37:18 AM
Sunday, November 1, 2020 7:39:34 PM
Sunday, September 27, 2020 11:48:38 PM
I want the time formats of the variable $time to change to "yyyy-mm-dd hh:mm:ss" so that ALL of the list is displayed
PS C:\Users\> $Time
2020-11-30 08:55:01
2020-10-18 11:10:01
2020-11-08 10:40:34
2020-11-08 11:47:37
2020-11-08 10:59:57
2020-12-01 03:15:42
2020-11-30 07:00:32
2020-11-30 12:19:06
2020-11-30 07:01:34
2020-12-01 01:12:10
2020-12-01 02:37:18
2020-11-01 07:39:34
2020-09-27 11:48:38
Please help me creating a code for the same
thanks
This is a pretty common question that gets asked here on StackOverflow, however, it seems most of the answers are directed towards converting a variable which stores a single date, to a formatted string.
Whereas you have an array of dates you want to convert.
I'm going to make the assumption that you have an Array of DateTime values, and not an Array of String.
For starters, there's TONS of blogs and articles about this, not to mention the documentation.
https://devblogs.microsoft.com/scripting/formatting-date-strings-with-powershell/
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/get-date
Depending on how you need to use this data there is a million different ways to do this.
Primarily, you need to learn how to perform actions against an array of objects. Using things like ForEach, ForEach-Object, Select-Object, etc. Once you learn how to use those, then the problem just becomes "how do you format a date to a string", which is all over the place on here and the rest of the internet.
Here's some examples:
# Use this to generate sample data:
$Time = 10000,9000,8000,7000,6000,5000,4000,3000,2000,1000 |
ForEach-Object { (Get-Date).AddMinutes(-$_) }
## Various solutions:
$Time | ForEach-Object { $_.ToString('yyyy-MM-dd HH:mm:ss') }
$Time | ForEach-Object { $_ | Get-Date -Format 'yyyy-MM-dd HH:mm:ss' }
$Time | ForEach-Object { $_ | Get-Date -f 'yyyy-MM-dd HH:mm:ss' }
$Time | ForEach-Object { Get-Date $_ -f 'yyyy-MM-dd HH:mm:ss' }
$Time | Select-Object #{N='TimeString'; E={$_.ToString('yyyy-MM-dd HH:mm:ss')}}
foreach ($tv in $time) { $tv.ToSTring('yyyy-MM-dd HH:mm:ss') }
$Time.ForEach({$_.ToString('yyyy-MM-dd HH:mm:ss')})
# Other methods submitted in comments, thanks #iRon
$Time | ForEach-Object ToString('yyyy-MM-dd HH:mm:ss')
$Time.ForEach('ToString', 'yyyy-MM-dd HH:mm:ss')
Note that this is case sensitive.
MM - Means the two digit month value
mm - Means the two digit day value

For a given month and year how to get the second Sunday using PowerShell?

I am trying to calculate the second Sunday of a month.
using PowerShell script, how to get the second Sunday of a month for any given year?
Using the integer value of Dayofweek as a (negative) offset 0..6 for Sunday..Saturday.
Get the Last Sunday of the previous Month and add 14
## Q:\Test\2019\04\29\SO_55905125.ps1
function Get-Sunday2nd {
param(
[int]$Year=(Get-Date).Year,
[int]$Month=(Get-Date).Month,
[switch]$Time)
$LastDayPrevMonth = (Get-Date -Year $Year -Month $Month -Day 1).Date.AddDays(-1)
$Sunday2nd = $LastDayPrevMonth.AddDays(14-[int]$LastDayPrevMonth.DayOfWeek)
$Sunday2nd
}
Get-Sunday2nd 2019 5
Sonntag, 12. Mai 2019 00:00:00
Get-Sunday2nd -Year 2018 -Month 5
Sonntag, 13. Mai 2018 00:00:00

Get the last day of a month on powershell

I'm trying to put in a .txt file the first day and the last day of the months using PowerShell.
In the exemple below i was trying to get the first and the last day of July, but i'm just getting the first day. The second line of the script isn't working.
#PowerShell "(Get-Date).AddMonths(1).ToString('01/MM/yyyy')" >>dates.txt
$LastDayInMonthString = "$($(get-date).AddMonths(1).ToString("dd/mm/yyy"))$LastDayInMonth" >>dates.txt
Someone can say me what is wrong?
I wanted a .txt file like it: 01/07/2018, 31/07/2018.
The first line write the first day of next month,
and second line write the last day of that month.
Much simpler solution is to call into the DaysInMonth function
[DateTime]::DaysInMonth(2018, 11)
For the current month that would look like:
$today = get-date
$lastDay = [DateTime]::DaysInMonth($today.Year, $today.Month)
$firstDate = [DateTime]::new($today.Year, $today.Month, 1)
$lastDate = [DateTime]::new($today.Year, $today.Month, $lastDay)
$firstDate
$lastDate
This also works around any hindering daylight savings changes and other weird things that can happen with timezones etc.
Or if pure strings are all you need:
(get-date -Format "yyyy/MM") + "/1"
(get-date -Format "yyyy/MM") + "/" + [DateTime]::DaysInMonth((get-date).Year, (get-date).Month)
An easy way is to take the last day of the previous year and add 1..12 months to it:
1..12 | % { (New-Object DateTime(2017,12,31)).AddMonths($_) }
Output will be in the user's date/time format, in my case Dutch:
woensdag 31 januari 2018 00:00:00
woensdag 28 februari 2018 00:00:00
zaterdag 31 maart 2018 00:00:00
maandag 30 april 2018 00:00:00
donderdag 31 mei 2018 00:00:00
zaterdag 30 juni 2018 00:00:00
dinsdag 31 juli 2018 00:00:00
vrijdag 31 augustus 2018 00:00:00
zondag 30 september 2018 00:00:00
woensdag 31 oktober 2018 00:00:00
vrijdag 30 november 2018 00:00:00
maandag 31 december 2018 00:00:00
If required you can format it as you need it, e.g.
1..12 | % { (New-Object DateTime(2017,12,31)).AddMonths($_).ToString("yyyyMMdd") }
20180131
20180228
20180331
20180430
20180531
20180630
20180731
20180831
20180930
20181031
20181130
20181231
This seems simple enough
$firstDate = [DateTime]::new($reportDate.Year, $reportDate.Month, 1)
$lastDate=$firstDate.AddMonths(1).AddDays(-1)
Edit removed the for only date unneccessary time adjustments
In PowerShell to get the first day and last of next month
$CIGB = New-Object System.Globalization.CultureInfo('en-GB')
'{0}, {1}' -f (Get-Date -Day 1).AddMonths(1).ToString('d',$CIGB),
(Get-Date -Day 1).AddMonths(2).AddDays(-1).ToString('d',$CIGB)|sc dates.txt
The $CIGB is neccessary for me because my local date separator overrides the /
If your short date format 'd' returns dd/MM/yyyy the first line and the ,$CIGB can be removed.
01/07/2018, 31/07/2018
This can be wrapped in a single (albeit quite long) line.
powershell -nop -c "$CIGB=New-Object System.Globalization.CultureInfo('en-GB');'{0}, {1}' -f (Get-Date -Day 1).AddMonths(1).ToString('d',$CIGB),(Get-Date -Day 1).AddMonths(2).AddDays(-1).ToString('d',$CIGB)">>dates.txt
> type dates.txt
01/07/2018, 31/07/2018
Strings can be passed to Get-Date. The -Format parameter also accepts strings/variables.
# Get the previous month
$LastMonth = (Get-Date).AddMonths(-1)
# Format the first day of the month
$FirstDayOfMonth = $LastMonth | Get-Date -Format "yyyy/MM/01"
# Calculate the number of days (last day) in the month
$LastDay = [DateTime]::DaysInMonth($LastMonth.Year, $LastMonth.Month)
# Use it in the format parameter
$LastDayOfMonth = $LastMonth | Get-Date -Format "yyyy/MM/$lastDay"

Get-Date formatting/culture

How do I specify what part of my input string is the date and month?
If the input is 01/10/2017, this can be read as 1st Oct 2017 and 10th Jan 2017. Both are correct.
I want to be explicit that 01 is date and 10 is month, so that irrespective of locale and time format I can get a consistent result.
Sample code:
get-date -Date '01/10/2017'
The output is:
Tuesday, January 10, 2017 12:00:00 AM
The desired output is:
Sunday, October 01, 2017 12:00:00 AM
I have a solution for you. It requires that the culture as one of the arguments.
([datetime]::ParseExact($date,"dd/MM/yyyy",[Globalization.CultureInfo]::CreateSpecificCulture('en-GB')))
A culture does not have to be specified. However, the argument for it does, otherwise you will get an error:
Cannot find an overload for "ParseExact" and the argument count: "2".
[cultureinfo]::InvariantCulture or $null can be used as the third argument:
$date = "01/10/2017"
[datetime]::ParseExact($date, "dd/MM/yyyy", [cultureinfo]::InvariantCulture)
[datetime]::ParseExact($date, "dd/MM/yyyy", $null)
Output in all three cases
01 October 2017 00:00:00
Try this:
Get-Date(Get-Date -Date $date -Format 'dd/MM/yyyy')
You can enforce the culture for single commands (or command blocks). This should help avoiding that date chaos.
PS C:\> [System.Threading.Thread]::CurrentThread.CurrentUICulture = "en-US" ; [System.Threading.Thread]::CurrentThread.CurrentCulture = "en-US"; get-date -Date '01/10/2017'
Tuesday, January 10, 2017 12:00:00 AM
PS C:\> [System.Threading.Thread]::CurrentThread.CurrentUICulture = "en-GB" ; [System.Threading.Thread]::CurrentThread.CurrentCulture = "en-GB"; get-date -Date '01/10/2017'
01 October 2017 00:00:00

PowerShell date time conversion issue

When I run the below two PowerShell statements, I expect them to return the same result, but in fact they return different results:
New-TimeSpan $(Get-Date) $(Get-Date -month 12 -day 31 -year 2016)
New-TimeSpan $(Get-Date) $(Get-Date "2016-12-31")
The results are:
Days : 751
Hours : 0
Minutes : 0
Seconds : 0
Milliseconds : 1
Ticks : 648864000010001
TotalDays : 751.000000011575
TotalHours : 18024.0000002778
TotalMinutes : 1081440.00001667
TotalSeconds : 64886400.0010001
TotalMilliseconds : 64886400001.0001
Days : 750
Hours : 6
Minutes : 50
Seconds : 13
Milliseconds : 67
Ticks : 648246130673175
TotalDays : 750.284873464323
TotalHours : 18006.8369631437
TotalMinutes : 1080410.21778863
TotalSeconds : 64824613.0673175
TotalMilliseconds : 64824613067.3175
What's going on?
I'm pretty sure the answer is in the different parameter sets you are using. Referring to the TechNet article. There are two different parameter sets: net and uformat
Get-Date "2016-12-31" is is being detected as net
(Get-Date -month 12 -day 31 -year 2016) is being detected as uformat
In the latter, since you are not specifying the time components, it is defaulting them to the current time. Looking at the documentation for -Hour
-Hour<Int32>
Specifies the hour that is displayed. Enter a value from 1 to 23. The default is the current hour.
When the string "2016-12-31" is cast to a datetime midnight, it is used in the absence of a value. In the other case no time is specified, so it is using the current time in its place.
PS C:\Users\Cameron> (Get-Date -month 12 -day 31 -year 2016)
Saturday, December 31, 2016 5:14:32 PM
PS C:\Users\Cameron> $(Get-Date "2016-12-31")
Saturday, December 31, 2016 12:00:00 AM
I might be slightly wrong on the terms or cause, but I have to be close. Even when specifying hour for example the other time values still need to come from somewhere. The time when the code was run was 5:24 pm EST.
PS C:\Users\Cameron> (Get-Date -month 12 -day 31 -year 2016 -Hour 12)
Saturday, December 31, 2016 12:24:43 PM