Date time parsing to another format - date

I need to get the end date of the month we are in.
I tried with this code:
$CURRENTDATE=GET-DATE -Format "dd/MM/yyyy"
$FIRSTDAYOFMONTH=GET-DATE $CURRENTDATE -Day 1
$LASTDAYOFMONTH=GET-DATE $FIRSTDAYOFMONTH.AddMonths(1).AddSeconds(-1)
echo $LASTDAYOFMONTH
This gives me the date of the end of the month. But i need it in another format (dd/MM/yyyy).
But can't seem to get my head around it. A push in the right direction would be great.

Adapting Mike F. Robbins code
$currentDate = Get-Date
$lastDay = [DateTime]::DaysInMonth($currentDate.Year, $currentDate.Month)
$lastDayOfMonth = Get-Date ([DateTime]"$($currentDate.Month), $LastDay, $($currentDate.Year)") -Format 'dd/MM/yyyy'
Write-Host $lastDayOfMonth

Or if you want to use your existing code:
$CurrentDate = Get-Date -Format "dd/MM/yyyy"
$FirstDayOfMonth = Get-Date $CurrentDate -Day 1
$LastDayOfMonth = Get-Date $FirstDayOfMonth.AddMonths(1).AddSeconds(-1) -Format "dd/MM/yyyy"
Write-Output $LastDayOfMonth

Related

reference to datetime in powershell

$var = "01/01/2020"
$date1 = Get-Date
$date2 = Get-Date
Write-Host $date1.GetType().Name
Write-Host $date2.GetType().Name
$dates = #(
#('date1', [ref]$date1),
#('date2', [ref]$date2)
)
foreach($date in $dates) {
$date[1].Value = Get-Date ([DateTime]::ParseExact($var, 'dd/MM/yyyy', $null)) -Format "dd/MM/yyyy"
}
Write-Host $date1
Write-Host $date2
Write-Host $date1.GetType().Name
Write-Host $date2.GetType().Name
output :
DateTime
DateTime
01/01/2020
01/01/2020
String
String
I do not understand why my dates (date1 and date2) went from DateTime to String ?
How do I fix it ? because the next step in my code is to compare date1 and date2 (I want to compare dates not string obviously)
Help is much appreciate
This is the problem
$date[1].Value = Get-Date ([DateTime]::ParseExact($var, 'dd/MM/yyyy', $null)) -Format "dd/MM/yyyy"
Remove the -Format "dd/MM/yyyy" from your code.
You are parsing a string to a date using [DateTime]::ParseExact then immediately convert it back to a string using -Format "dd/MM/yyyy".
You just need
$date[1].Value = Get-Date ([DateTime]::ParseExact($var, 'dd/MM/yyyy', $null))

Get-Date -Format is giving different result to Get-Date

I am getting date and time in a variable which is different from my Server clock.
QDate = $(Get-Date -Format "yyyyMMdd hh:MM:ss tt")
If I simply type Get-Date I can get the server local time but I am using this variable to insert time in SQL Database and I need the dates in given format.
> Get-Date -Format "yyyyMMdd hh:MM:ss tt"
20190425 10:04:39
> Get-Date
April 25, 2019 10:07:28 AM
time difference 04 minutes
You've got...
Get-Date -Format "yyyyMMdd hh:MM:ss tt"
You want...
Get-Date -Format "yyyyMMdd hh:mm:ss tt"
Note, the change in case of the "mm" for minutes,

Error with formatting a date when using addhours()

I'm using Powershell v2 to add hours to a date, but I always get an error.
This code below work fine:
$date = Get-Date -format "yyyyMMddhhmmss"
$DateStr ='{0:yyyyMMddhhmmss}' -f $Date
Write-Host $date
But when I use addhours():
$oriDate=(Get-Date).addhours(1)
$date = $oriDate -format "yyyyMMddhhmmss"
$DateStr ='{0:yyyyMMddhhmmss}' -f $Date
Write-Host $date
I get the error:
You must provide a value expression on the right-hand side of the '-f' operator.
There is a simpler way to get what you're after. Check out the Get-Date reference page for examples and generally useful info.
(Get-Date).ToString("yyyyMMddhhmmss")
$((Get-Date).AddHours(1)).ToString("yyyyMMddhhmmss")

Date format not applied when doing AddDay

I've got the following two queries:
$startDate = (Get-Date -format s).AddDays(-6)
$endDate = (Get-Date -format s)
Write-Host $startDate
Write-Host $endDate
//Prints
2015-05-02 16:23:52
2015-05-08T16:47:56
I'd like to understand how to put the T back in the first printed dates. The space breaks my powershell script where I add that date to an LogParser.sql path:
Myquery.Sql?startdate='$startdate'+enddate='$enddate')
This line:
$startDate = (Get-Date -format s).AddDays(-6)
throws an error (about [system.string] not having a function AddDays) for me with powershell 3.0.
I can get the output you want from this though:
"{0:s}" -f (Get-Date).AddDays(6)

Check if date in past

$olddate = Get-Date -Date "31-dec-2013 00:00:00" -Format d.M.yyyy
$Now = Get-date -Format d.M.yyyy
How can I check if $olddate is earlier than $Now?
If your store them as DateTime instead of as formatted strings, you can use the less-than operator (-lt) just like with regular numbers, and then use the format operator (-f) when you need to actually display the date:
$olddate = Get-Date -Date "31-dec-2013 00:00:00"
$Now = Get-Date
if($olddate -lt $Now){
"`$olddate is in the past!"
"{0:d.M.yyyy}" -f $olddate
"{0:d.M.yyyy}" -f $Now
}
You can also do a comparison on Ticks. I.e.: $olddate.Ticks will be -lt then $Now.Ticks.