Solved

Powershell Date comparison operator help

Posted on 2014-12-19
7
966 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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

[Webinar] Disaster Recovery and Cloud Management

Learn from Unigma and CloudBerry industry veterans which providers are best for certain use cases and how to lower cloud costs, how to grow your Managed Services practice in IaaS clouds, and how to utilize public cloud for Disaster Recovery

Question has a verified solution.

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

Synchronize a new Active Directory domain with an existing Office 365 tenant
A procedure for exporting installed hotfix details of remote computers using powershell
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

920 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

13 Experts available now in Live!

Get 1:1 Help Now