Solved

Powershell Date comparison operator help

Posted on 2014-12-19
7
877 Views
Last Modified: 2014-12-23
I am having trouble trying to figure out how I can compare Dates.
I have a script that will compare Today's late to determine if it's earlier than a date specified from a drop-down menu (win forms), but the problem is, something like 12/19/2014 is going to be classified as "greater than" 1/1/2015".

What date formatting/values do I need to have today set as so that 12/19/2014 is returned as "earlier than" 1/1/2015? I can't seem to use string values or less than/greater than comparison operators because it doesn't seem to be possible.

$date_SingleStart = New-Object System.Windows.Forms.DateTimePicker
$date_SingleStart.Font = New-Object System.Drawing.Font("Tahoma", 9.75, [System.Drawing.FontStyle]::Regular, [System.Drawing.GraphicsUnit]::Point, ([System.Byte](0)))
$date_SingleStart.Format = [System.Windows.Forms.DateTimePickerFormat]::Short
$date_SingleStart.Location = New-Object System.Drawing.Point(5, 84)
$date_SingleStart.Size = New-Object System.Drawing.Size(158, 23)
$date_SingleStart.TabIndex = 3

$date_SingleEnd = New-Object System.Windows.Forms.DateTimePicker
$date_SingleEnd.CustomFormat = "MM-dd-yyyy"
$date_SingleEnd.Font = New-Object System.Drawing.Font("Tahoma", 9.75, [System.Drawing.FontStyle]::Regular, [System.Drawing.GraphicsUnit]::Point, ([System.Byte](0)))
$date_SingleEnd.Format = [System.Windows.Forms.DateTimePickerFormat]::Short
$date_SingleEnd.Location = New-Object System.Drawing.Point(169, 84)
$date_SingleEnd.Size = New-Object System.Drawing.Size(158, 23)
$date_SingleEnd.TabIndex = 4

## I have button between that when pushed then goes with the following logic:


$currentDate = Get-Date
$currentDateString = $currentDate.ToString("MM-dd-yyyy")	

$single_StartDate = $date_SingleStart.value.ToString("MM-dd-yyyy")
$single_EndDate = $date_SingleEnd.value.ToString("MM-dd-yyyy")

if ($single_StartDate -lt $currentDateString)
	{		
		try {
		some_Function
}
catch { 
write-host "Error"
}

Open in new window

0
Comment
Question by:garryshape
  • 3
  • 2
  • 2
7 Comments
 
LVL 16

Accepted Solution

by:
Rajitha Chimmani earned 250 total points
ID: 40509592
You have to compare dates as datetime object only to obtain desired results. i could not test how it works in forms but if its possible to give the date format as below in the form then its easier. Try below and compare it against get-date

[datetime]$date_SingleEnd.CustomFormat = "MM/dd/yyyy"
0
 

Author Comment

by:garryshape
ID: 40509640
HI Rajitha Chimmani
Would that be in the GUI properties or when I'm setting new variables based on the GUI calendar picker?
0
 
LVL 39

Assisted Solution

by:footech
footech earned 250 total points
ID: 40509791
Rajitha is absolutely correct.  You could have something like this.
$currentDate = Get-Date

$single_StartDate = $date_SingleStart.value
$single_EndDate = $date_SingleEnd.value

if ($single_StartDate -lt $currentDate)
	{		
		try {
		some_Function
}

Open in new window


There's no need for the intermediate variables here though.
if ($date_SingleStart.value -lt (Get-Date))
	{		
		try {
		some_Function
}

Open in new window

0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 16

Expert Comment

by:Rajitha Chimmani
ID: 40509797
No, it should be in the below command. you are defining the date format in the below command. Sorry, I overlooked and gave the above line.

$date_SingleEnd.CustomFormat = "MM/dd/yyyy"

The get-date command does not have MM-dd-yyyy format in the list. And you are converting the date to string and comparing them which would not produce desired results.

Also, you have defined the custom format only for End date and this line is not there for the start date. Any particular reason for that? Or did you get this script from somewhere else?
0
 

Author Comment

by:garryshape
ID: 40509845
Thanks much for the advice here, I will try it out when I get a chance. Unfortunately I can't try again until this Sunday afternoon so don't worry if I haven't replied back then.

As for the strings, I converted date values to strings because they are later used in e-mail messages. Not sure if I need them converted to strings to accomplish that, or if I just need to create additional variables for the e-mail message purposes...

e-mail body: "Hello sir, your start date is $date_SingleStart and your end date is $date_SingleEnd" for example
0
 
LVL 39

Expert Comment

by:footech
ID: 40509889
For the later use in an email, yes it is better to convert them to strings.  If you include a [datetime] object in a string it will be converted automatically, but the format may not be what you want.
0
 

Author Comment

by:garryshape
ID: 40515248
Here's the PowerShell GUI prog I made that the date comparisons helped with.
It lets me add and schedule mailbox permissions on the Exchange server, uses task scheduler to run pre-made script files generated from the program, and schedules accordingly.

Not too complicated but a great time saver in my environment.

mbx permissions gui
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

The article will show you how you can maintain a simple logfile of all Startup and Shutdown events on Windows servers and desktops with PowerShell. The script can be easily adapted into doing more like gracefully silencing/updating your monitoring s…
A procedure for exporting installed hotfix details of remote computers using powershell
This video discusses moving either the default database or any database to a new volume.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now