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

x
?
Solved

Date Arithmetic example Powershell and VB Script

Posted on 2010-09-01
9
Medium Priority
?
971 Views
Last Modified: 2012-05-10
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
0
Comment
Question by:Westez
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 13

Assisted Solution

by:soostibi
soostibi earned 500 total points
ID: 33580627
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
 
LVL 59

Assisted Solution

by:Bill Prew
Bill Prew earned 500 total points
ID: 33580662
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
 
LVL 6

Assisted Solution

by:rwskas
rwskas earned 500 total points
ID: 33580791
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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

Author Comment

by:Westez
ID: 33583590
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
 
LVL 71

Expert Comment

by:Chris Dent
ID: 33584368

> 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
 
LVL 71

Accepted Solution

by:
Chris Dent earned 500 total points
ID: 33584419

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
 
LVL 59

Expert Comment

by:Bill Prew
ID: 33585494
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
 

Author Comment

by:Westez
ID: 33601005
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
 

Author Closing Comment

by:Westez
ID: 33621112
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

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Simple Linear Regression

926 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