Date Arithmetic example Powershell and VB Script

I'm asking for examples of date arithmetic using Powershell and VB Script.  Just some basic stuff, nothing fancy, I need some help in getting started.

If date ne  to today then do something, move a file, delete a file, etc.
If date eq  to today then do something, move a file, delete a file, etc.

Thanks
WestezAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Chris DentConnect With a Mentor PowerShell DeveloperCommented:

Minor edit, the date above should be changed to todays day, not yesterdays :)

A more comprehensive example below.

Chris
' This is already a date
Date_1 = #09/02/2010#
' This includes Time
Date_2 = Now()
' This does not include Time
Date_3 = Date()

' Test to see if these are equal
If Date_1 = Date_2 Then
  Date_1_and_2 = "Equal"
Else
  Date_1_and_2 = "Not Equal"
End If

' Test to see if these are equal
If Date_1 = Date_3 Then
  Date_1_and_3 = "Equal"
Else
  Date_1_and_3 = "Not Equal"
End If

WScript.Echo "Date 1: " & Date_1 & vbCrLf & _
  "Date 2: " & Date_2 & vbCrlf & _
  "Date 3: " & Date_3 & VbCrlf & VbCrLf & _
  "Date 1 and 2: " & Date_1_and_2 & VbCrLf & _
  "Date 1 and 3: " & Date_1_and_3 & VbCrlf & VbCrlf & _
  "DateDiff 1 and 2: " & DateDiff("d", Date_1, Date_2) & VbCrLf & _
  "DateDiff 1 and 3: " & DateDiff("d", Date_1, Date_3)

Open in new window

0
 
soostibiConnect With a Mentor Commented:
I'd rather do the following. Let's assume, that the $date variable contains the date that you would like to check:

$date = [datetime] "2010.09.01"

Then the '$date is today' check is this:
if(((get-date) - $date).days -eq 0){"this is today"}

$date is not today is this:
if(((get-date) - $date).days -ne 0){"this is not today"}
0
 
Bill PrewConnect With a Mentor Commented:
Fairly easy in VBS, you can do things like:

d=CDate(#4/22/10#)
If d = Now() Then
  wscript.echo "Equal"
Else
  wscript.echo "Not Equal"
End If

You also might want to look at the DateDiff() function, it can be very useful.  A nice consice reference here:

http://www.w3schools.com/vbscript/vbscript_ref_functions.asp#date

~bp
0
Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

 
rwskasConnect With a Mentor Commented:
Just an example.. This would be similar to how you would say delete files older than x - using only 1 if statement ofcourse
$Today = Get-Date
$Minus15Days = (Get-Date).AddDays(-15)
$Minus45Minutes = (Get-Date).AddMinutes(-45)
$Files = Get-ChildItem C:\Temp

Foreach ($File in $Files) {
$Timestamp = $File.LastWriteTime
	If ($TimeStamp -le $Today) { "$File is older that today" }
		If ($TimeStamp -le $Minus15Days) { "$File is older than 15 days" }
			if ($Timestamp -le $Minus45Minutes) { "$File is older than 45 Minutes"}
}

Open in new window

0
 
WestezAuthor Commented:
First let me say thanks for helping me out.

Soostibi - your code worked straight out.

Bill - I changed the date in the code to today's date and it returns Not Equal.  I was expecting Equal. I saved the file with the name date1.vbs.  Am I making a mistake with the date?

d=CDate(#09/01/2010#)
If d = Now() Then
  wscript.echo "Equal"
Else
  wscript.echo "Not Equal"
End If


PS C:\scripts> cscript date1.vbs
Microsoft (R) Windows Script Host Version 5.7
Copyright (C) Microsoft Corporation. All rights reserved.

Not Equal

rwskas - The files that begin with the word date have today's time stamp on them.  The date3.ps1 file, I just copied and pasted your code into notepad and saved it.  The date6.ps1 file, I changed the operator from le to ne and got a different result.  I was expecting the files with today's time stamp to be excluded from the output after I changed the operator.  Would you clue me in as to what's up with the results?

PS C:\scripts> c:\scripts\date3.ps1

date1.vbs is older than today
date1.vbs is older than 45 Minutes
date2.ps1 is older than today
date2.ps1 is older than 45 Minutes
date3.ps1 is older than today
date3.ps1 is older than 45 Minutes
folder permissions.txt is older than today
folder permissions.txt is older than 45 Minutes
folderpermissions.vbs is older than today
folderpermissions.vbs is older than 45 Minutes
iislog2.ps1 is older than today
iislog2.ps1 is older than 45 Minutes
iis_log_manage_example.txt is older than today
iis_log_manage_example.txt is older than 45 Minutes
iis_log_manage_example2.txt is older than today
iis_log_manage_example2.txt is older than 45 Minutes
shop.ps1 is older than today
shop.ps1 is older than 45 Minutes

PS C:\scripts> c:\scripts\date6.ps1

date1.vbs is older than today
date1.vbs is older than 15 days
date1.vbs is older than 45 Minutes
date2.ps1 is older than today
date2.ps1 is older than 15 days
date2.ps1 is older than 45 Minutes
date3.ps1 is older than today
date3.ps1 is older than 15 days
date3.ps1 is older than 45 Minutes
folder permissions.txt is older than today
folder permissions.txt is older than 15 days
folder permissions.txt is older than 45 Minutes
folderpermissions.vbs is older than today
folderpermissions.vbs is older than 15 days
folderpermissions.vbs is older than 45 Minutes
iislog2.ps1 is older than today
iislog2.ps1 is older than 15 days
iislog2.ps1 is older than 45 Minutes
iis_log_manage_example.txt is older than today
iis_log_manage_example.txt is older than 15 days
iis_log_manage_example.txt is older than 45 Minutes
iis_log_manage_example2.txt is older than today
iis_log_manage_example2.txt is older than 15 days
iis_log_manage_example2.txt is older than 45 Minutes
shop.ps1 is older than today
shop.ps1 is older than 15 days
shop.ps1 is older than 45 Minutes
PS C:\scripts>
0
 
Chris DentPowerShell DeveloperCommented:

> Am I making a mistake with the date?

Yes, you're forgetting time. These are the values you're comparing and they're not equal.

WScript.Echo CDate(#09/01/2010#)
WScript.Echo Now()


Soostibi's version sidesteps issues with the Time by measuring days only from a TimeSpan (what you get back when you take one date away from another). If you want to use the same method in VbScript you need DateDiff:


Date1 = CDate(#09/01/2010#)
Date2 = Now()
WScript.Echo DateDiff("d", Date1, Date2)


Where d tells it to return the difference in Days.

Chris
0
 
Bill PrewCommented:
Sorry, in my example if you just wanted to compare to the data, then it should be:

d=CDate(#4/22/10#)
If d = Date() Then
  wscript.echo "Equal"
Else
  wscript.echo "Not Equal"
End If

There are two system functions that can be used, Date() returns just the current date, Now() returns the current date and the current time.  Sorry for the confusion.

~bp
0
 
WestezAuthor Commented:
Thanks for details guys.  I'm going to pick back up on this later tonight.  I need a bit more time to dig into this and work with the code.
0
 
WestezAuthor Commented:
All - thanks for helping me out here, this is a good start.  I don't find stuff like this when I search the web.  
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.