Solved

Powershell Date comparison operator help

Posted on 2014-12-19
7
1,254 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 40

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
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
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 40

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

Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

Question has a verified solution.

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

Utilizing an array to gracefully append to a list of EmailAddresses
Synchronize a new Active Directory domain with an existing Office 365 tenant
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
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…

739 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