Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Powershell Date comparison operator help

Posted on 2014-12-19
7
Medium Priority
?
1,503 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 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
Are You Ready for GDPR?

With the GDPR deadline set for May 25, 2018, many organizations are ill-prepared due to uncertainty about the criteria for compliance. According to a recent WatchGuard survey, a staggering 37% of respondents don't even know if their organization needs to comply with GDPR. Do you?

 
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

Introducing the WatchGuard 420 Access Point

WatchGuard's newest access point includes an 802.11ac Wave 2 chipset, providing the fastest speeds for VoIP, video and music streaming, and large data file transfers. Additionally, enjoy the benefits of strong security as the 3rd radio delivers dedicated WIPS protection!

Question has a verified solution.

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

A procedure for exporting installed hotfix details of remote computers using powershell
A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
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…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

718 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