Check if date in past - powershell

$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.

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))

Powershell script to check if given hours is between working hours

What I've done is to get the current hour and then compare it to a string that I get as an output from a system that I split to get the starting hour and ending hour.
The check keeps showing up as True no matter what the time is
$current_day = Get-Date -Format "dddd"
$current_hour = Get-Date -Format "HH:mm"
$string = "Sat 16:00 to 00:00".Split(" ")
$day = $string[0]
$from_hour = Get-Date $string[1] -Format "HH:mm"
$to_hour = Get-Date $string[3] -Format "HH:mm"
Write-Output("CURRENT TIME: {0} {1}" -f $current_day, $current_hour)
Write-Output("GIVEN TIME: {0} from {1} to {2}" -f $string_day, $string_from_hour, $string_to_hour)
if($current_hour -le $from_hour -and $current_hour -ge $to_hour)
{
return $true
}
else
{
return $false
}
Your code does work with just a few small changes.
The most critical is just swapping the operators around for your if statement
Write-Output("GIVEN TIME: {0} from {1} to {2}" -f $string_day, $from_hour, $to_hour)
if($current_hour -ge $from_hour -and $current_hour -le $to_hour)
Finally, instead of comparing to 00:00 midnight, change the end time to 23:59 which is close enough for government work. With these two things done, it works.
CURRENT TIME: Thursday 19:13
GIVEN TIME: from 16:00 to 23:59
True
You can try something like this:
$StartTime = "16:00";
$EndTime = "00:00"
$EndTime_ = (Get-Date $EndTime).AddDays(1);
if (((Get-Date) -gt (Get-Date $StartTime)) -and ((Get-Date) -lt ($EndTime_))) {
Get-Date -Format "hh:mm";
}
else {
$false
}
Thanks everyone for their suggestions.
I couldn't miss a minute as the script is intended to run per hour.
So what I've done is to convert 00:00 into 99:99 (both current time and working hours) and it works flawlessly.

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 time parsing to another format

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

powershell : how to format get-date to string and remove 0's?

I'm attempting to eliminate leading any leading zeroes in my date when I run the get-date cmdlet by trying:
$filedate = get-date -uformat "%m-%d-%Y"
$filedate = $filedate.ToString().Replace("0", "")
this returns "01-04-2008"
I want to the output to be "1-4-2008"
any ideas on another way of doing this?
thanks in advance
$filedate = get-date -format "M-d-yyyy"
$fileDate = $fileDate.ToString("M-d-yyyy")
use format string like :
get-date -format yyyy/M/d
or
get-date.tostring(yyyy/M/d)