Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if date in past

Tags:

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?

like image 840
mzv Avatar asked Feb 12 '15 09:02

mzv


People also ask

How to determine if date has passed compared to today’s date?

Supposing the current date is 2015/5/25, with the following formula, you can determine if date has passed compared with today’s date. Please do as follows. 1. Select a blank cell which is adjacent to the date you want to determine if it is overdue. Enter the below formula into it, and then press the Enter key. 2.

How to find out if a date has passed in Excel?

Excel Determine if Dates Have Passed 1 Select the range with dates you want to compare with current date and select the overdue dates,... 2 In the Select Specific Cells dialog box, select Cell in the Selection type section,... 3 Then a Select Specific Cells prompt box pops up to tell you how many cells will be selected,... See More....

How do you use a date in an IF statement?

Using the IF Function with DATEVALUE Function If you want to use a date in your IF function’s logical test, you can wrap the date in the DATEVALUE function. This function converts a date in text format to a serial number that Excel can recognize as a date. If you put a date within quotes, it is essentially a text or string value.

How to calculate future or past dates in Excel?

Calculate Future or Past Dates in Excel Using IF Formula In this example, we will check if a date is in a range or not. For example, take today’s day into account. The motive of this example is to check whether the delivery will take place or not within ten days.


2 Answers

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
}
like image 60
Mathias R. Jessen Avatar answered Sep 22 '22 12:09

Mathias R. Jessen


You can also do a comparison on Ticks. I.e.: $olddate.Ticks will be -lt then $Now.Ticks.

like image 44
David Brabant Avatar answered Sep 23 '22 12:09

David Brabant