Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Powershell Date comparison operator help

Posted on 2014-12-19
7
Medium Priority
?
1,598 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 1000 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 41

Assisted Solution

by:footech
footech earned 1000 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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
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 41

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

Lessons on Wi-Fi & Recommendations on KRACK

Simplicity and security can be a difficult  balance for any business to tackle. Join us on December 6th for a look at your company's biggest security gap. We will also address the most recent attack, "KRACK" and provide recommendations on how to secure your Wi-Fi network today!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
Measuring Server's processing rate with a simple powershell command. The differences in processing rate also was recorded in different use-cases, when a server in free and busy states.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the adminiā€¦
Screencast - Getting to Know the Pipeline

916 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