Rob van der Woude's Scripting Pages

Date and Time

Non-batch solutions

Sometimes it may be advisable to use other tools or languages to achieve your goals.
Just take a look at these JScript, KiXtart, Perl, Rexx and VBScript scripts that use only a few lines to display sorted ate and time, week number, Easter's or yesterday's date or check for leap years.

JScript:

KiXtart:

Perl:

Modify the first line of each Perl script for use in non-Windows environments.

PowerShell:

Rexx:

Note: These Rexx scripts may have the extension .CMD (OS/2 Rexx) or .REX (Regina Rexx).
Most of the scripts presented here were created in OS/2 but do not use OS/2 specific code, so they may be used unaltered in Regina Rexx.
Yesterday.rex, however, was written specifically for Regina Rexx and uses some non-OS/2 Rexx features.

VBScript:

Download the sources

Some examples using these scripts in NT batch files.

Back to the top of this page... ]


Easter

Easter calculates the date of Easter Day for the specified year.

Easter.js

// Easter.js,  Version 1.11 for WSH 1.0
// Calculate Easter Day for a specified year
// Written by Rob van der Woude
// http://www.robvanderwoude.com

// Define variable to insert CR/LF
jsCrLf = String.fromCharCode( 13, 10 );

// Determine current year
objToday = new Date();
thisYear = objToday.getFullYear();

// Check command line parameters
objArgs = WScript.Arguments;
if ( objArgs.length == 0 ) {
	// Default is current year
	myYear = thisYear;
};
if ( objArgs.length == 1 ) {
	// Check datatype and range of argument
	myYear = objArgs(0).valueOf( );
	if ( isNaN( myYear ) ) Syntax( );
	// Convert argument string to number
	myYear = Math.floor( myYear );
	if ( myYear < 1752 )   Syntax( );
	if ( myYear > 3000 )   Syntax( );
};
if ( objArgs.length > 1 ) Syntax( );


// Calculate Easter Day using the instructions found at
// Simon Kershaw's "KEEPING THE FEAST"
// http://www.oremus.org/liturgy/etc/ktf/app/easter.html
G   = ( myYear % 19 ) + 1;
S   = Math.floor(( myYear - 1600 ) / 100 ) - Math.floor(( myYear - 1600 ) / 400 );
L   = Math.floor(( Math.floor(( myYear - 1400 ) / 100 ) * 8 ) / 25 );
PP  = ( 30003 - ( 11 * G ) + S - L ) % 30;
if ( PP == 28 ) {
	if ( G > 11 ) {
		P = 27;
	}
} else {
	if ( PP == 29 ) {
		P = 28;
	} else {
		P = PP;
	}
}
D   = ( Math.floor( myYear / 4 ) - Math.floor( myYear / 100 ) + Math.floor( myYear / 400 ) + myYear ) % 7;
DD  = ( 8 - D ) % 7;
PPP = ( 70003 + P ) % 7;
X   = (( 70004 - D - P ) % 7 ) + 1;
E   = P + X;
if ( E < 11 ) {
	ED = E + 21;
	EM = "March";
} else {
	ED = E - 10;
	EM = "April";
}
if ( myYear < thisYear ) {
	strIS = "was";
} else {
	if ( myYear == thisYear ) {
		strIS = "is";
	} else {
		strIS = "will be";
	}
}

// Display the result
WScript.Echo( jsCrLf + "In " + myYear + " Easter Day " + strIS + " " + EM + " " + ED );

// Done
WScript.Quit(0);


function Syntax( ) {
	strMsg  = jsCrLf + "Easter.js,  Version 1.11 for WSH 1.0" + jsCrLf;
	strMsg += "Calculate the date of Easter Day for the specified year.";
	strMsg += jsCrLf + jsCrLf;
	strMsg += "Usage:  CSCRIPT  EASTER.JS  [ year ]";
	strMsg += jsCrLf + jsCrLf;
	strMsg += "Where:  year should be within the range 1752 through 3000";
	strMsg += jsCrLf + jsCrLf;
	strMsg += "Written by Rob van der Woude" + jsCrLf;
	strMsg += "http://www.robvanderwoude.com";
	strMsg += jsCrLf + jsCrLf;
	strMsg += "Based on the instructions found at" + jsCrLf;
	strMsg += "Simon Kershaw's 'KEEPING THE FEAST'" + jsCrLf;
	strMsg += "http://www.oremus.org/liturgy/etc/ktf/app/easter.html";
	WScript.Echo( strMsg );
	WScript.Quit(1);
}

Back to the top of this page... ]

Easter.kix

If $Year = ""
	$Year = @YEAR
EndIf

; Is the specified year valid?
; Check if number and if within range
If VarType($Year) = 1 OR VarType($Year) <> 3 OR $Year < 1752 OR $Year > 3000
	GOTO Syntax
EndIf

; Calculate Easter Day using the instructions found at
; Simon Kershaw's "KEEPING THE FEAST"
; http://www.oremus.org/liturgy/etc/ktf/app/easter.html
$G  = Mod( $Year, 19 ) + 1
$S  = (( $Year - 1600 ) / 100 ) - (( $Year - 1600 ) / 400 )
$L  = ((( $Year - 1400 ) / 100 ) * 8 ) / 25
$P1 = Mod(( 30003 - 11 * $G + $S - $L ), 30)
Select
	Case $P1 = 28 AND $G > 11
		$P = 28
	Case $P1 = 29
		$P = 28
	Case 1
		$P = $P1
EndSelect
$D  = Mod(( $Year + ( $Year / 4 ) - ( $Year / 100 ) + ( $Year / 400 )), 7 )
$D1 = Mod(( 8 - $D ), 7 )
$P2 = Mod(( 70003 + $P ), 7 )
$X  = Mod(( 70004 - $D - $P ), 7 ) + 1
$E  = $P + $X
If $E < 11
	$ED = $E + 21
	$EM = "March"
Else
	$ED = $E - 10
	$EM = "April"
EndIf
Select
	Case $Year < @YEAR
		$IS = "was"
	Case $Year = @YEAR
		$IS = "is"
	Case 1
		$IS = "will be"
EndSelect

; Display the result
? "In $Year Easter Day $IS $EM $ED"
?

; MOD function divides $Op1 by $Op2 and returns the remainder
Function Mod($Op1,$Op2)
	$Mod = $Op1 - $Op2 * ( $Op1 / $Op2 )
EndFunction

; End of main program
Exit 0


:Syntax
? "Easter.kix,  Version 1.00"
? "Calculate the date of Easter Day for the specified year."
?
? "Usage:  KIX32  EASTER.KIX  [ $$YEAR=year ]"
?
? "Where:  year should be within the range of 1752 through 3000"
?
? "Written by Rob van der Woude"
? "http://www.robvanderwoude.com"
?
? "Based on the instructions found at"
? "Simon Kershaw's " + Chr(34) + "KEEPING THE FEAST" + Chr(34)
? "http://www.oremus.org/liturgy/etc/ktf/app/easter.html"
?
Exit 1

Back to the top of this page... ]

Easter2.kix

; Check KiXtart version (4.20 required)
$KixMajorVer = SubStr( @Kix, 1, InStr( @Kix, "." ) - 1 )
$KixMinorVer = SubStr( @Kix, InStr( @Kix, "." ) + 1, 2 )
$KixVer      = ( 100 * $KixMajorVer ) + $KixMinorVer
If $KixVer < 420
	GoTo Syntax
EndIf

; Use current year if none specified
If $Year = ""
	$Year = @YEAR
EndIf

; Is the specified year valid?
; Check if number and if within range
If VarType($Year) = 1 OR VarType($Year) <> 3 OR $Year < 1752 OR $Year > 3000
	GoTo Syntax
EndIf

; Calculate Easter Day using the instructions found at
; Simon Kershaw's "KEEPING THE FEAST"
; http://www.oremus.org/liturgy/etc/ktf/app/easter.html
$G  = ( $Year Mod 19 ) + 1
$S  = (( $Year - 1600 ) / 100 ) - (( $Year - 1600 ) / 400 )
$L  = ((( $Year - 1400 ) / 100 ) * 8 ) / 25
$P1 = ( 30003 - 11 * $G + $S - $L ) Mod 30
Select
	Case $P1 = 28 AND $G > 11
		$P = 28
	Case $P1 = 29
		$P = 28
	Case 1
		$P = $P1
EndSelect
$D  = ( $Year + ( $Year / 4 ) - ( $Year / 100 ) + ( $Year / 400 )) Mod 7 
$D1 = ( 8 - $D ) Mod 7
$P2 = (( 70003 + $P ) Mod 7 )
$X  = (( 70004 - $D - $P ) Mod 7 ) + 1
$E  = $P + $X
If $E < 11
	$ED = $E + 21
	$EM = "March"
Else
	$ED = $E - 10
	$EM = "April"
EndIf
Select
	Case $Year < @YEAR
		$IS = "was"
	Case $Year = @YEAR
		$IS = "is"
	Case 1
		$IS = "will be"
EndSelect

; Display the result
? "In $Year Easter Day $IS $EM $ED"
?

; End of main program
Exit 0


:Syntax
? "Easter.kix,  Version 2.00 for KiXtart 4.2*"
? "Calculate the date of Easter Day for the specified year."
?
? "Usage:  KIX32  EASTER.KIX  [ $$YEAR=year ]"
?
? "Where:  year should be within the range of 1752 through 3000"
?
? "Written by Rob van der Woude"
? "http://www.robvanderwoude.com"
?
? "Based on the instructions found at"
? "Simon Kershaw's " + Chr(34) + "KEEPING THE FEAST" + Chr(34)
? "http://www.oremus.org/liturgy/etc/ktf/app/easter.html"
?
? "This script requires KiXtart 4.20 or later."
? "Your KiXtart version is @Kix"
?
Exit 1

Back to the top of this page... ]

Easter.pl

#! perl

# Check command line parameters
if ( !$ARGV[0] ) {
	($sec,$min,$hour,$mday,$mon,$ChkYear,$wday,$yday,$isdst) = localtime(time);
	$ChkYear = $ChkYear + 1900;
} else {
	if ( $ARGV[1] ) {
		Syntax( );
	} else {
		$strChkYear = $ARGV[0];
		$ChkYear = abs( $strChkYear );
		if ( $strChkYear != $ChkYear ) {
			Syntax( );
		}
		if ( ( $ChkYear < 1752 ) or ( $ChkYear > 3000 ) ) {
			Syntax( );
		}
	}
}

# Calculate Easter Day using the instructions found at
# Simon Kershaw's "KEEPING THE FEAST"
# http://www.oremus.org/liturgy/etc/ktf/app/easter.html

$G  = ( $ChkYear % 19 ) + 1;
$S  = int( ( $ChkYear - 1600 ) / 100 ) - int( ( $ChkYear - 1600 ) / 400 );
$L  = int( ( int( ( $ChkYear - 1400 ) / 100 ) * 8 ) / 25 );
$PP = ( 30003 - 11 * $G + $S - $L ) % 30;
if ( ( ( $PP == 28 ) and ( $G > 11 ) ) or ( $PP == 29 ) ) {
	$P = $PP - 1;
} else {
	$P = $PP;
}
$D   = ( $ChkYear + int( $ChkYear / 4 ) - int( $ChkYear / 100 ) + int( $ChkYear / 400 )) % 7;
$DD  = ( 8 - $D ) % 7;
$PPP = ( 70003 + $P ) % 7;
$X   = (( 70004 - $D - $P ) % 7 ) + 1;
$E   = $P + $X;
if ( $E < 11 ) {
	$ED = $E + 21;
	$EM = "March";
} else {
	$ED = $E - 10;
	$EM = "April";
}

# Use default if no (valid) year was specified
if ( ( $year < 0 ) or ( $year > 10000 ) or !( $year ) ) {
	# Parse time string
	($s,$m,$h,$md,$mo,$thisYear,$wd,$yd,$isdst) = localtime(time);
	$thisYear = $thisYear + 1900;
}
if ( $ChkYear < $thisYear ) {
	$Is = "was";
} else {
	if ( $ChkYear == $thisYear ) {
		$Is = "is";
	} else {
		$Is = "will be";
	}
}

# Display the result
print( "\nIn $ChkYear Easter Day $Is $EM $ED\n" );

# Done
exit 0;


sub Syntax {
	print "\nEaster.pl,  Version 1.00\n",
	      "Calculate the date of Easter Day for the specified year\n\n",
	      "Usage:  EASTER.PL  [ year ]\n\n",
	      "Where:  year should be within the range of 1752 through 3000",
	      "\n\nWritten by Rob van der Woude\n",
	      "http://www.robvanderwoude.com\n\n",
	      "Based on the instructions found at\n",
	      "Simon Kershaw\'s \"KEEPING THE FEAST\"",
	      "http://www.oremus.org/liturgy/etc/ktf/app/easter.html\n";
	exit 1;
}

Back to the top of this page... ]

Easter.ps1

# Easter is at first Sunday after the first full moon at or after the Spring equinox (21 March)
# Calculation explained: https://www.timeanddate.com/calendar/determining-easter-date.html
Set-Variable -Name ReferenceFullMoon -Option Constant -Value $( Get-Date -Date '2022-01-17 23:48' ) -ErrorAction Ignore
Set-Variable -Name MoonCycleDays     -Option Constant -Value 29.53                                  -ErrorAction Ignore
if ( $args.Length -eq 1 ) {
	$year = $args[0] # enclose year in quotes if less than 100, and always use 4 digits, e.g. '0033'
} else {
	$year = $( Get-Date ).Year # or specify any other year on the command line
}
# For Easter calculations, the Spring equinox is always assumed to be at 21 March
$springequinox = $( Get-Date -Date "$year-03-21" ) # 21 March
# Calculate full moon cycles to first full moon after Spring equinox of specified year
$fullcycles = [Math]::Ceiling( ( New-TimeSpan -Start $ReferenceFullMoon -End $springequinox ).Days / $MoonCycleDays )
# Date of first full moon after Spring equinox of specified year
$fullmoonafterequinox = $ReferenceFullMoon.AddDays( $fullcycles * $MoonCycleDays )
# First Sunday following first full moon at or after Spring equinox of specified year
$eastersunday = $fullmoonafterequinox.AddDays( 7 - [int]$fullmoonafterequinox.DayOfWeek ).Date
# Display the result
if ( ( New-TimeSpan -Start ( Get-Date ).Date -End $eastersunday ).Days -lt 0 ) {
	"Easter {0} was at {1}" -f $year, $eastersunday.ToLongDateString( )
} else {
	"Easter {0} will be at {1}" -f $year, $eastersunday.ToLongDateString( )
}

Back to the top of this page... ]

Easter.rex

/* Easter.rex,  Version 1.00 for Regina Rexx */
/* Calculate Easter Day for a specified year */
/* Written by Rob van der Woude              */
/* http://www.robvanderwoude.com             */

/* Check command line parameters */
Parse Arg year dummy
If dummy <> "" Then Call Syntax
thisYear = Substr( Date( "S" ), 1, 4 )
If year = "" Then year = thisYear
If DataType( year, "W" ) = 0 Then Call Syntax
If year < 1752 Then Call Syntax
If year > 3000 Then Call Syntax

/* Calculate Easter Day using the instructions found at  */
/* Simon Kershaw's "KEEPING THE FEAST"                   */
/* http://www.oremus.org/liturgy/etc/ktf/app/easter.html */
G  = year // 19 + 1
S  = ( year - 1600 ) % 100 - ( year - 1600 ) % 400
L  = ( ( ( year - 1400 ) % 100 ) * 8 ) % 25
P1 = ( 30003 - 11 * G + S - L ) // 30
P  = P1
If P = 28 Then If G > 11 Then P = 27
If P = 29 Then P = 28
D  = ( year + ( year % 4 ) - ( year % 100 ) + ( year % 400 )) // 7
D1 = ( 8 - D ) // 7
P2 = ( 70003 + P ) // 7
X  = ( ( 70004 - D - P ) // 7 ) + 1
E  = P + X
If E < 11 Then Do
	ED = E + 21
	EM = "March"
End
Else Do
	ED = E - 10
	EM = "April"
End
If year < thisYear Then is = "was"
If year = thisYear Then is = "is"
If year > thisYear Then is = "will be"

/* Display the result */
Say "In "||year||" Easter Day "||is||" "||EM||" "||ED

/* Computer, end program */
Exit 0

Syntax:
	Say
	Say "Easter.rex,  Version 1.00 for Regina Rexx"
	Say "Calculate the date of Easter Day for the specified year."
	Say
	Say "Usage:  Regina  EASTER.REX  [ year ]"
	Say
	Say "Where:  year should be within the range of 1752 through 3000"
	Say
	Say "Written by Rob van der Woude"
	Say "http://www.robvanderwoude.com"
	Say
	Say "Based on the instructions found at"
	Say "Simon Kershaw's "||'"KEEPING THE FEAST"'
	Say "http://www.oremus.org/liturgy/etc/ktf/app/easter.html"
	Exit 1
Return

Back to the top of this page... ]

Easter.vbs

' Easter.vbs,  Version 1.01 for WSH 1.0
' Calculate Easter Day for a specified year
' Written by Rob van der Woude
' http://www.robvanderwoude.com

' Check command line parameters
Select Case WScript.Arguments.Count
	Case 0
		ChkYear = Year(Date)
	Case 1
		strChkYear = Wscript.Arguments(0)
		On Error Resume Next
		ChkYear = CInt(strChkYear)
		If Err.Number <> 0 Then
			Syntax
			Wscript.Quit(1)
		End If
		If VarType(ChkYear) <> 2 Then
			Syntax
			Wscript.Quit(1)
		End If
		ChkYear = CInt(Wscript.Arguments(0))
		If ChkYear < 1752 Or ChkYear > 3000 Then
			Syntax
			Wscript.Quit(1)
		End If
	Case Else
		Syntax
		Wscript.Quit(1)
End Select

' Calculate Easter Day using the instructions found at
' Simon Kershaw's "KEEPING THE FEAST"
' http://www.oremus.org/liturgy/etc/ktf/app/easter.html
G   = ( ChkYear Mod 19 ) + 1
S   = (( ChkYear - 1600 ) \ 100 ) - (( ChkYear - 1600 ) \ 400 )
L   = ((( ChkYear - 1400 ) \ 100 ) * 8 ) \ 25
PP  = ( 30003 - 11 * G + S - L ) Mod 30
Select Case PP
	Case 28
		If G > 11 Then P = 27
	Case 29
		P = 28
	Case Else
		P = PP
End Select
D   = ( ChkYear + ( ChkYear \ 4 ) - ( ChkYear \ 100 ) + ( ChkYear \ 400 )) Mod 7
DD  = ( 8 - D ) Mod 7
PPP = ( 70003 + P ) Mod 7
X   = (( 70004 - D - P ) Mod 7 ) + 1
E   = P + X
If E < 11 Then
	ED = E + 21
	EM = "March"
Else
	ED = E - 10
	EM = "April"
End If
thisYear = Year(Date)
If ChkYear < thisYear Then
	strIS = "was"
ElseIf ChkYear = thisYear Then
	strIS = "is"
Else
	strIS = "will be"
End If

' Display the result
Wscript.Echo vbCrLf & "In " & ChkYear & " Easter Day " & strIS & " " & EM & " " & ED

' Done
Wscript.Quit(0)



Sub Syntax
msg = vbCrLf & "Easter.vbs,  Version 1.01 for WSH 1.0" & vbCrLf & _
      "Calculate the date of Easter Day for the specified year." & vbCrLf & _
      vbCrLf & "Usage:  CSCRIPT  EASTER.VBS  [ year ]" & vbCrLf & vbCrLf & _
      "Where:  year should be within the range of 1752 through 3000" & _
      vbCrLf & vbCrLf & _
      "Written by Rob van der Woude" & vbCrLf & _
      "http://www.robvanderwoude.com" & vbCrLf & vbCrLf & _
      "Based on the instructions found at" & vbCrLf & _
      "Simon Kershaw's " & Chr(34) & "KEEPING THE FEAST" & Chr(34) & _
      vbCrLf & "http://www.oremus.org/liturgy/etc/ktf/app/easter.html"
Wscript.Echo(msg)
End Sub

Back to the top of this page... ]

LeapYear

LeapYear checks if the specified or current year is a leap year.

LeapYear.js

// LeapYear.js,  Version 1.01 for Windows Script Host
// Check if the specified year is a leap year.
//
// Written by Rob van der Woude
// http://www.robvanderwoude.com

// Parse command line
objArgs = WScript.Arguments;
if ( objArgs.length == 0 ) {
	// Default is current year
	objToday = new Date();
	myYear = objToday.getFullYear();
};
if ( objArgs.length == 1 ) {
	// Check datatype and range of argument
	myYear = objArgs(0).valueOf( );
	if ( isNaN( myYear ) ) Syntax( );
	if ( myYear <    0 )   Syntax( );
	if ( myYear > 9999 )   Syntax( );
};
if ( objArgs.length > 1 ) Syntax( );

isLeapYear = 0;
if ( myYear %   4 == 0 ) isLeapYear = 1;
if ( myYear % 100 == 0 ) isLeapYear = 0;
if ( myYear % 400 == 0 ) isLeapYear = 1;

if ( isLeapYear == 1 ) {
	strIs = " IS";
} else {
	strIs = " is NOT";
};
WScript.Echo( myYear + strIs + " a leap year" );

// Done
WScript.Quit( isLeapYear );


function Syntax( ) {
	WScript.Echo( );
	WScript.Echo( "LeapYear.js,  Version 1.01 for WSH" );
	WScript.Echo( "Check if the specified year is a leap year." );
	WScript.Echo( );
	WScript.Echo( "Usage:   CScript  LEAPYEAR.JS  [ year ]" );
	WScript.Echo( );
	WScript.Echo( "Where:   'year' should be within the range of 0 through 9999." );
	WScript.Echo( "         Default is the current year, if none is specified." );
	WScript.Echo( );
	WScript.Echo( "Returns: 0  if NOT a leap year" );
	WScript.Echo( "         1  on leap years" );
	WScript.Echo( "         2  on syntax errors" );
	WScript.Echo( );
	WScript.Echo( "Written by Rob van der Woude" );
	WScript.Echo( "http://www.robvanderwoude.com" );
	WScript.Quit(2);
}

Back to the top of this page... ]

LeapYear.kix

; LeapYear.kix,  Version 1.00
; Check if the specified or current year is a leap year

; Default is current year
If $Year = ""
	$Year = @YEAR
EndIf

; Check if the specified parameter is a number
If Val( $Year ) = 0
	If "$Year" <> "0"
		GoTo Syntax
	EndIf
EndIf

; Check if the specified year is within range
If $Year < 1
	GoTo Syntax
EndIf
If $Year > 9999
	GoTo Syntax
EndIf

; OK, continue
$Year = Val( $Year )

; Initialize
$LeapYear = 0
; A leap year is any multiple of 4...
$Test = $Year / 4
$Test = $Test * 4
If $Year = $Test
	$LeapYear = 1
EndIf
; ...except if it is a multiple of 100...
$Test = $Year / 100
$Test = $Test * 100
If $Year = $Test
	$LeapYear = 0
EndIf
; ...unless it is a multiple of 400!
$Test = $Year / 400
$Test = $Test * 400
If $Year = $Test
	$LeapYear = 1
EndIf

; Display the result
$Is = "IS"
If $LeapYear = 0
	$Is = "is NOT"
EndIf
? "$Year $Is a leap year"

; Exit with proper return code
Quit $LeapYear


:Syntax
? "LeapYear.kix,  Version 1.00"
? "Check if the specified year is a leap year."
?
? "Usage:   KIX32  LEAPYEAR.KIX  [ $$Year=year ]"
?
? "Where:   " + Chr(34) + "year" + Chr(34)
" should be within the range of 1 through 9999."
? "         Default is the current year, if none is specified."
?
? "Returns: 0  if NOT a leap year"
? "         1  on leap years"
? "         2  on syntax errors"
?
? "Note:    Due to KiXtart's command line parsing, specifying the year 0 on the"
? "         commandline will have the same effect as not specifying a year at all."
?
? "Written by Rob van der Woude"
? "http://www.robvanderwoude.com"
?
Quit 2

Back to the top of this page... ]

LeapYear.pl

#! perl

if ( ( index( $ARGV[0], "?" ) > -1 ) or ( $ARGV[1] ) or ( $ARGV[0] < 0 ) or ( $ARGV[0] > 10000 ) or ( ( $ARGV[0] ) and ( $ARGV[0] == NaN ) ) ) {
	print "\nLeapYear.pl,  Version 1.10\n",
	      "Check if the specified year is a leap year\n\n",
	      "Usage:    LEAPYEAR.PL  [ year ]\n\n",
	      "Where:    \"year\" is a year between 1 and 10000\n",
	      "          default is the current year\n\n",
	      "Returns:  message on screen;\n",
	      "          return code 0 if the specified year is not a leap year;\n",
	      "          return code 1 if it is;\n",
	      "          return code 9 for invalid arguments\n\n",
	      "Written by Rob van der Woude\n",
	      "http://www.robvanderwoude.com\n";
	exit 9;
} else {
	$year = abs( $ARGV[0] );
}

# Use default if no (valid) year was specified
if ( ( $year < 0 ) or ( $year > 10000 ) or !( $year ) ) {
	# Parse time string
	($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
	$year = $year + 1900;
}

# Leap years are divisible by 4, but not by 100, unless by 400
if ( ( $year % 4 == 0 ) xor ( $year % 100 == 0 ) xor ( $year % 400 == 0 ) ) {
	print( "\n$year IS a leap year\n" );
	$rc = 1;
} else {
	print( "\n$year is NOT a leap year\n" );
	$rc = 0;
}

# Terminate with return code 1 if the year IS a leap year
exit $rc

Back to the top of this page... ]

LeapYear.ps1

#
# LeapYear.ps1,  Version 1.00
#
# Usage:  .\LEAPYEAR.PS1  [ year ]
#
# Where:  "year"  is the year which you want to test
#                 (default is current year)
#
# Written by Rob van der Woude
# http://www.robvanderwoude.com
#

# Get the specified year
param( [int]$year = ( Get-Date ).Year )

# Get the current year
[int]$thisyear = ( Get-Date ).Year

# Format the output text (past, present or future?)
$is1 = "is"
$is2 = ""
If ( $year -lt $thisyear ) {
	$is1 = "was"
}
If ( $year -gt $thisyear ) {
	$is1 = "will"
	$is2 = " be"
}

# Check if the specified year is a leap year
[boolean]$leapyear = ( [boolean]!( $year % 4 ) -and [boolean]( $year % 100 ) ) -or [boolean]!( $year % 400 )

# Display the result
If ( $leapyear ) {
	Write-Host "$year $is1$is2 a leap year"
}
Else {
	Write-Host "$year $is1 NOT$is2 a leap year"
}

Back to the top of this page... ]

LeapYear.rex

/* LeapYear.rex                               */
/* Check if the specified year is a leap year */
/* Written by Rob van der Woude               */
/* http://www.robvanderwoude.com              */

parse arg year dummy .
if dummy <> "" then call Syntax
if year  <> "" then do
	if pos( "?", year ) >    0 then call Syntax
	if year             <    0 then call Syntax
	if year             > 9999 then call Syntax
end
else do
	year = substr( date("S"), 1, 4 )
end

leapyear = 0
if year //   4 = 0 then leapyear = 1
if year // 100 = 0 then leapyear = 0
if year // 400 = 0 then leapyear = 1

if leapyear = 1 then do
	msg = year||" IS "
end
else do
	msg = year||" is NOT "
end
msg = msg||"a leap year"
say
say msg
exit 0


Syntax:
	say
	say "LeapYear.rex,  Version 1.00"
	say "Check if the specified year is a leap year"
	say
	say "Usage:  <REXX>  LEAPYEAR.REX  [ year ]"
	say
	say 'Where:  "<REXX>" is your Rexx interpreter:'
	say "                 - Windows:  REGINA.EXE or REXX.EXE, whichever you installed"
	say "                 - OS/2:     no need to specify, just rename script to *.cmd"
	say '        "year"   is any year between 0 and 9999'
	say
	say "Written by Rob van der Woude"
	say "http://www.robvanderwoude.com"
	exit 1
return

Back to the top of this page... ]

LeapYear.vbs

' LeapYear.vbs,  Version 1.00 for Windows Script Host
' Check if the specified year is a leap year.
'
' Written by Rob van der Woude
' http://www.robvanderwoude.com

Select Case WScript.Arguments.Count
	Case 0
		' Default is current year
		MyYear = Year( Date )
	Case 1
		' Check datatype and range of argument
		On Error Resume Next
		MyYear = CLng( WScript.Arguments(0) )
		If Err.Number Then Syntax
		Err.Clear
		On Error Goto 0
		If MyYear < 0    Then Syntax
		If MyYear > 9999 Then Syntax
	Case Else
		Syntax
End Select

LeapYear = 0
If MyYear Mod   4 = 0 Then LeapYear = 1
If MyYear Mod 100 = 0 Then LeapYear = 0
If MyYear Mod 400 = 0 Then LeapYear = 1

strIs = " IS"
If LeapYear = 0 Then strIs = " is NOT"
WScript.Echo( MyYear & strIs & " a leap year" )

WScript.Quit( LeapYear )


Sub Syntax
msg = vbCrLf & "LeapYear.vbs,  Version 1.00 for WSH 1.0" & vbCrLf & _
      "Check if the specified year is a leap year." & vbCrLf & _
      vbCrLf & "Usage:   CScript  LEAPYEAR.VBS  [ year ]" & vbCrLf & vbCrLf & _
      "Where:   " & Chr(34) & "year" & Chr(34) & _
      " should be within the range of 0 through 9999." & vbCrLf & _
      "         Default is the current year, if none is specified." & _
      vbCrLf & vbCrLf & _
      "Returns: 0  if NOT a leap year" & vbCrLf & _
      "         1  on leap years" & vbCrLf & _
      "         2  on syntax errors" & vbCrLf & vbCrLf & _
      "Written by Rob van der Woude" & vbCrLf & _
      "http://www.robvanderwoude.com"
Wscript.Echo( msg )
WScript.Quit(2)
End Sub

Back to the top of this page... ]

SetDate

SetDate sets a couple of (system) environment variables with the current date's day, month, year, week, quarter, weekday and day of the year. If scheduled daily and at every system startup, batch files can read these date "components" directly from environment variables, instead of each batch file having to calculate it again.

SetDate.vbs

Option Explicit

Dim dtmNow
Dim strDay, strDoW, strDoY, strMonth, strQuarter, strWeek, strYear
Dim wshShell, wshSystemEnv

Set wshShell = CreateObject( "WScript.Shell" )

If WScript.Arguments.Count > 0 Then Syntax

Set wshSystemEnv = wshShell.Environment( "SYSTEM" )

' Take a "snapshot" of the current date and time,
' because setting the variables may take quite a while
dtmNow = Now

' Set the "components" in environment variables
strDay     = Right( "0" & DatePart( "d", dtmNow ), 2 )
strDoW     = DatePart( "w", dtmNow )
strDoY     = Right( "00" & DatePart( "y", dtmNow ), 3 )
strMonth   = Right( "0" & DatePart( "m", dtmNow ), 2 )
strQuarter = DatePart( "q", dtmNow )
strWeek    = Right( "0" & DatePart( "ww", dtmNow ), 2 )
strYear    = DatePart( "yyyy", dtmNow )

wshSystemEnv( "Date.Day"     ) = strDay
wshSystemEnv( "Date.DoW"     ) = strDoW
wshSystemEnv( "Date.DoY"     ) = strDoY
wshSystemEnv( "Date.Month"   ) = strMonth
wshSystemEnv( "Date.Quarter" ) = strQuarter
wshSystemEnv( "Date.Week"    ) = strWeek
wshSystemEnv( "Date.Year"    ) = strYear

Set wshSystemEnv = Nothing
Set wshShell     = Nothing


Sub Syntax
	WScript.Echo vbCrLf _
	           & "SetDate.vbs,  Version 1.10" _
	           & vbCrLf _
	           & "Set the current date in a set of environment variables" _
	           & vbCrLf & vbCrLf _
	           & "Usage:  CSCRIPT  SETDATE.VBS" _
	           & vbCrLf & vbCrLf _
	           & "Notes:  Schedule this command every day at midnight AND at system startup." _
	           & vbCrLf _
	           & "        From then on, all batch files and programs will have a set of" _
	           & vbCrLf _
	           & "        environment variables available that can be used in log files names" _
	           & vbCrLf _
	           & "        etcetera." _
	           & vbCrLf _
	           & "        The following variables will be available (with the number of digits" _
	           & vbCrLf _
	           & "        between parentheses):" _
	           & vbCrLf _
	           & "        Date.Day     (2)    Date.DoW  (1)    Date.DoY  (2)    Date.Month (2)" _
	           & vbCrLf _
	           & "        Date.Quarter (1)    Date.Week (2)    Date.Year (4)" _
	           & vbCrLf & vbCrLf _
	           & "Written by Rob van der Woude" _
	           & vbCrLf _
	           & "http://www.robvanderwoude.com"
	WScript.Quit 1
End Sub

Back to the top of this page... ]

SortDate

SortDate displays the current date in YYYYMMDD format.

SortDate.js

// SortDate.js,  Version 1.00 for Windows Script Host 1.00
// Show current date in YYYYMMDD format
// Written by Rob van der Woude
// http://www.robvanderwoude.com

// Get and parse current date
now   = new Date();
day   = now.getDate();
month = now.getMonth() + 1;
year  = now.getYear();

// Add leading spaces if necessary
if ( day   < 10 ) day   = "0" + day;
if ( month < 10 ) month = "0" + month;

// Concatenate strings and display the result
WScript.Echo( "SortDate=" + year + "" + month + "" + day );

Back to the top of this page... ]

SortDate.kix

; SortDate.kix,  Version 1.01
; Displays date in YYYYMMDD
; Written by Rob van der Woude
; http://www.robvanderwoude.com

$sdate = substr( @date, 1, 4 ) + substr( @date, 6, 2 ) + substr( @date, 9, 2 )
? $sdate

Back to the top of this page... ]

SortDate.pl

#! perl

# SortDate.pl,  Version 2.00
# Display "sorted" date (YYYYMMDD)
# Written by Rob van der Woude
# http://www.robvanderwoude.com

# Parse time string
($day,$month,$year) = (localtime)[3,4,5];

# Add "base year"
$year  = $year  + 1900;

# Add 1, since month is zero based
$month = $month + 1;

# Display result, correctly formatted
printf("\nSortDate = %04d%02d%02d\n", $year, $month, $day);

Back to the top of this page... ]

Today.ps1

This PowerShell script combines SortDate, Tomorrow and Yesterday.

""
"Date / Format   YYYYMMDD        DD-MM-YYYY        MM/DD/YYYY"
"============================================================"
"Yesterday       " + (get-date (get-date).AddDays(-1) -uformat %Y%m%d) + "        " + (get-date (get-date).AddDays(-1) -uformat %d-%m-%Y) + "        " + (get-date (get-date).AddDays(-1) -uformat %m/%d/%Y)
"Today           " + (get-date -uformat %Y%m%d)                        + "        " + (get-date (get-date)             -uformat %d-%m-%Y) + "        " + (get-date (get-date)             -uformat %m/%d/%Y)
"Tomorrow        " + (get-date (get-date).AddDays(1)  -uformat %Y%m%d) + "        " + (get-date (get-date).AddDays(1)  -uformat %d-%m-%Y) + "        " + (get-date (get-date).AddDays(1)  -uformat %m/%d/%Y)

Back to the top of this page... ]

SortDate.rex

/* Display date in YYYYMMDD */

say date( "S" )

Back to the top of this page... ]

SortDate.vbs

' SortDate.vbs,  Version 1.00 for Windows Script Host 2.00
' Display today's date in YYYYMMDD format
'
' Written by Rob van der Woude
' http://www.robvanderwoude.com

strSortDate = DatePart("yyyy",Date)
If DatePart("m",Date) < 10 Then
	strSortDate = strSortDate & "0"
End If
strSortDate = strSortDate & DatePart("m",Date)
If DatePart("d",Date) < 10 Then
	strSortDate = strSortDate & "0"
End If
strSortDate = strSortDate & DatePart("d",Date)

Wscript.Echo("SortDate=" & strSortDate)

Back to the top of this page... ]

SortTime

SortTime displays the current time in HHmm or HHmmss format.

SortTime.js

// SortTime.js,  Version 1.00 for Windows Script Host 1.00
// Show current time in HHmmss format
// Written by Rob van der Woude
// http://www.robvanderwoude.com

// Get and parse current time
now     = new Date();
hours   = now.getHours();
minutes = now.getMinutes();
seconds = now.getSeconds();

// Add leading spaces if necessary
if ( hours   < 10 ) hours   = "0" + hours;
if ( minutes < 10 ) minutes = "0" + minutes;
if ( seconds < 10 ) seconds = "0" + seconds;

// Concatenate strings and display the result
WScript.Echo( "SortTime=" + hours + "" + minutes + "" + seconds );

Back to the top of this page... ]

SortTime.kix

; SortTime.kix,  Version 1.02
; Displays time in HHMMSS
; Written by Rob van der Woude
; http://www.robvanderwoude.com

$stime = substr( @time, 1, 2 ) + substr( @time, 4, 2 ) + substr( @time, 7, 2 )
? $stime

Back to the top of this page... ]

SortTime.pl

#! perl

# SortTime.pl,  Version 2.00
# Display "sorted" time (HHmmSS)
# Written by Rob van der Woude
# http://www.robvanderwoude.com

# Parse time string
($sec,$min,$hour) = (localtime)[0,1,2];

# Display result, correctly formatted
printf("\nSortTime = %02d%02d%02d\n", $hour, $min, $sec);

Back to the top of this page... ]

SortTime.rex

/* Display time in HHMMSS */

sorthour = right( time( "H" ), 2, "0" )
sortmins = right( time( "M" ) - ( 60 * time( "H" ) ), 2, "0" )
sortsecs = right( time( "S" ) - ( 60 * time( "M" ) ), 2, "0" )
sorttime = sorthour||sortmins||sortsecs
say sorttime

Back to the top of this page... ]

SortTime.vbs

' SortTime.vbs,  Version 1.00 for Windows Script Host 2.00
' Display current time in HHmm format
'
' Written by Rob van der Woude
' http://www.robvanderwoude.com

If DatePart("h",Time) < 10 Then
	strSortTime = "0" & DatePart("h",Time)
Else
	strSortTime = DatePart("h",Time)
End If
If DatePart("n",Time) < 10 Then
	strSortTime = strSortTime & "0"
End If
strSortTime = strSortTime & DatePart("n",Time)

Wscript.Echo("SortTime=" & strSortTime)

Back to the top of this page... ]

Tomorrow

Tomorrow shows tomorrow's date in "sortable" format.

Tomorrow.js

// Tomorrow.js,  Version 1.00 for Windows Script Host 2.00
// Display today's, yesterday's and tomorrow's date in two formats
//
// Written by Rob van der Woude
// http://www.robvanderwoude.com

// Specify header
strHead1 = "Format:     YYYYMMDD  (DD/MM/YYYY)";
strHead2 = "==================================";

// Create Date object
objToday = new Date();

// Get today's year, month and day
year  = objToday.getFullYear();
month = objToday.getMonth() + 1;
if ( month < 10 )
	{
	month = "0" + month;
	};
day   = objToday.getDate();
if ( day < 10 )
	{
	day = "0" + day;
	};

// Format output for today
strToday = "Today:      " + year + month + day + "  (" + day;
strToday = strToday + "/" + month + "/" + year + ")";

// Get current date in milliseconds since January 1, 1970
today = objToday.valueOf();

// Subtract 1 day
oneDay = 1000 * 24 * 60 * 60;
yesterday = today - oneDay;
objToday.setTime(yesterday);

// Get yesterday's year, month and day
year  = objToday.getFullYear();
month = objToday.getMonth() + 1;
if ( month < 10 )
	{
	month = "0" + month;
	};
day   = objToday.getDate();
if ( day < 10 )
	{
	day = "0" + day;
	};

// Format output for yesterday
strYest = "Yesterday:  " + year + month + day + "  (" + day;
strYest = strYest + "/" + month + "/" + year + ")";

// Add 1 day
oneDay = 1000 * 24 * 60 * 60;
tomorrow = today + oneDay;
objToday.setTime(tomorrow);

// Get tomorrow's year, month and day
year  = objToday.getFullYear();
month = objToday.getMonth() + 1;
if ( month < 10 )
	{
	month = "0" + month;
	};
day   = objToday.getDate();
if ( day < 10 )
	{
	day = "0" + day;
	};

// Format output for tomorrow
strTomor = "Tomorrow:   " + year + month + day + "  (" + day;
strTomor = strTomor + "/" + month + "/" + year + ")";

// Display the result
WScript.Echo( strHead1 );
WScript.Echo( strHead2 );
WScript.Echo( strYest );
WScript.Echo( strToday );
WScript.Echo( strTomor );

Back to the top of this page... ]

Tomorrow.kix

; Tomorrow.kix,  Version 1.00
; Display today's and tomorrow's date in two formats
; Written by Rob van der Woude
; http://www.robvanderwoude.com

; Today's date in YYYYMMDD format
$SortDate = ( 10000 * @YEAR ) + ( 100 * @MONTHNO ) + @MDAYNO

; Calculate tomorrow's date
$TomorD = @MDAYNO + 1
$TomorM = @MONTHNO
$TomorY = @YEAR

; It gets tricky when today is the last day of the month
If @MDAYNO > 27
	Select
		Case @MONTHNO = 1 ; January
			If @MDAYNO = 31
				$TomorD = 1
				$TomorM = 2
			EndIf
		Case @MONTHNO = 2 ; February
			If @MDAYNO = 29
				$TomorD = 1
				$TomorM = 3
			Else
				If @MDAYNO = 28
					$LeapYear = 0
					If ( @YEAR / 4 ) * 4 = @YEAR
						$LeapYear = 1
					EndIf
					If ( @YEAR / 100 ) * 100 = @YEAR
						$LeapYear = 1
					EndIf
					If ( @YEAR / 400 ) * 400 = @YEAR
						$LeapYear = 1
					EndIf
					If $LeapYear = 1
						$TomorD = 1
						$TomorM = 3
					EndIf
				EndIf
			EndIf
		Case @MONTHNO = 3 ; March
			If @MDAYNO = 31
				$TomorD = 1
				$TomorM = 4
			EndIf
		Case @MONTHNO = 4 ; April
			If @MDAYNO = 30
				$TomorD = 1
				$TomorM = 5
			EndIf
		Case @MONTHNO = 5 ; May
			If @MDAYNO = 31
				$TomorD = 1
				$TomorM = 6
			EndIf
		Case @MONTHNO = 6 ; June
			If @MDAYNO = 30
				$TomorD = 1
				$TomorM = 7
			EndIf
		Case @MONTHNO = 7 ; July
			If @MDAYNO = 31
				$TomorD = 1
				$TomorM = 8
			EndIf
		Case @MONTHNO = 8 ; August
			If @MDAYNO = 31
				$TomorD = 1
				$TomorM = 9
			EndIf
		Case @MONTHNO = 9 ; September
			If @MDAYNO = 30
				$TomorD = 1
				$TomorM = 10
			EndIf
		Case @MONTHNO = 10 ; October
			If @MDAYNO = 31
				$TomorD = 1
				$TomorM = 11
			EndIf
		Case @MONTHNO = 11 ; November
			If @MDAYNO = 30
				$TomorD = 1
				$TomorM = 12
			EndIf
		Case @MONTHNO = 12 ; December
			If @MDAYNO = 31
				$TomorD = 1
				$TomorM = 1
				$TomorY = $TomorY + 1
			EndIf
	EndSelect
EndIf

; Tomorrow's date in YYYYMMDD format
$SortTomor = ( 10000 * $TomorY ) + ( 100 * $TomorM ) + $TomorD

If $TomorD < 10
	$TomorD = "0" + $TomorD
EndIf
If $TomorM < 10
	$TomorM = "0" + $TomorM
EndIf

; Display the results
? "Format:     YYYYMMDD  (DD/MM/YYYY)"
? "=================================="
? "Today:      $SortDate  (@MDAYNO/@MONTHNO/@YEAR)"
? "Tomorrow:   $SortTomor  ($TomorD/$TomorM/$TomorY)"
?

; Done
Exit

Back to the top of this page... ]

Tomorrow.pl

#! perl

# Tomorrow.pl,  Version 1.00
# Display tomorrow's "sorted" date (YYYYMMDD)
# Written by Rob van der Woude
# http://www.robvanderwoude.com

# Specify function library
use Time::Local;

# Convert today to epoch seconds
$today = timelocal(localtime);

# Add 1 day
@tomorrow = localtime($today + (24 * 60 * 60));

# Extract year, month and day, and correct base offsets
$year  = (@tomorrow)[5] + 1900;
$month = (@tomorrow)[4] + 1;
$day   = (@tomorrow)[3];

# Display the result, correctly formatted
printf("\nTomorrow = %04d%02d%02d\n", $year, $month, $day);

Back to the top of this page... ]

Tomorrow.rex

/* Tomorrow.rex,  Version 1.00 for Regina Rexx */
/* Show tomorrow's date in YYYYMMDD format     */
/* Written by Rob van der Woude                */
/* http://www.robvanderwoude.com               */

say
say "Tomorrow="||date( "S", date( "B" ) + 1, "B" )

Back to the top of this page... ]

Tomorrow.vbs

' Tomorrow.vbs,  Version 1.00 for Windows Script Host 2.00
' Display today's, yesterday's and tomorrow's date in two formats
'
' Written by Rob van der Woude
' http://www.robvanderwoude.com

' Specify header
strHead    = "Format:     YYYYMMDD  (DD/MM/YYYY)" & vbCrLf _
           & "==================================" & vbCrLf


' Get current year
strYear = DatePart("yyyy",Date)

' Get current month, add leading zero if necessary
If DatePart("m",Date) < 10 Then
	strMonth = 0 & DatePart("m",Date)
Else
	strMonth = DatePart("m",Date)
End If

' Get current day, add leading zero if necessary
If DatePart("d",Date) < 10 Then
	strDay = 0 & DatePart("d",Date)
Else
	strDay = DatePart("d",Date)
End If

' Format output for today
strToday = "Today:      " & strYear & strMonth & strDay _
         & "  (" & strDay & "/" & strMonth & "/" & strYear & ")" & vbCrLf

' Calculate yesterday's date
dtmYesterday = DateAdd("d",-1,Date)

' Get yesterday's year
strYear = DatePart("yyyy",dtmYesterday)

' Get yesterday's month, add leading zero if necessary
If DatePart("m",dtmYesterday) < 10 Then
	strMonth = 0 & DatePart("m",dtmYesterday)
Else
	strMonth = DatePart("m",dtmYesterday)
End If

' Get yesterday's day, add leading zero if necessary
If DatePart("d",dtmYesterday) < 10 Then
	strDay = DatePart("d",dtmYesterday)
Else
	strDay = DatePart("d",dtmYesterday)
End If

' Format output for yesterday
strYest = "Yesterday:  " & strYear & strMonth & strDay _
		& "  (" & strDay & "/" & strMonth & "/" & strYear & ")" & vbCrLf

' Calculate tomorrow's date
dtmTomorrow = DateAdd("d",1,Date)

' Get tomorrow's year
strYear = DatePart("yyyy",dtmTomorrow)

' Get tomorrow's month, add leading zero if necessary
If DatePart("m",dtmTomorrow) < 10 Then
	strMonth = 0 & DatePart("m",dtmTomorrow)
Else
	strMonth = DatePart("m",dtmTomorrow)
End If

' Get tomorrow's day, add leading zero if necessary
If DatePart("d",dtmTomorrow) < 10 Then
	strDay = 0 & DatePart("d",dtmTomorrow)
Else
	strDay = DatePart("d",dtmTomorrow)
End If

' Format output for tomorrow
strTomor = "Tomorrow:   " & strYear & strMonth & strDay & "  (" & _
          strDay & "/" & strMonth & "/" & strYear & ")"

' Display the result
Wscript.Echo( strHead & strYest & strToday & strTomor )

Back to the top of this page... ]

Week

Week displays the current week number.
Note that week number definitions may vary, and different scripts may display different week numbers.

Week.js

// Week.js,  Version 1.00 for Windows Script Host
// Display the current week number.
//
// Written by Rob van der Woude
// http://www.robvanderwoude.com

// Check command line parameters (none necessary)
objArgs = WScript.Arguments;
if ( objArgs.length > 0 ) Syntax( );

// Get and parse current date
objToday = new Date();
// Get day of month
numDate  = objToday.getDate( );
// Get day of week
numDay   = objToday.getDay( );
// Get month
numMonth = objToday.getMonth( ) + 1;
// Get year
numYear  = objToday.getFullYear();
// Check if this is a leap year
isLeapYear = 0;
if ( numYear %   4 == 0 ) isLeapYear = 1;
if ( numYear % 100 == 0 ) isLeapYear = 0;
if ( numYear % 400 == 0 ) isLeapYear = 1;
// Calculate the total number of days this year
numYDays = 0;
if ( numMonth >  1 ) numYDays = numYDays + 31;
if ( numMonth >  2 ) numYDays = numYDays + 28 + isLeapYear;
if ( numMonth >  3 ) numYDays = numYDays + 31;
if ( numMonth >  4 ) numYDays = numYDays + 30;
if ( numMonth >  5 ) numYDays = numYDays + 31;
if ( numMonth >  6 ) numYDays = numYDays + 30;
if ( numMonth >  7 ) numYDays = numYDays + 31;
if ( numMonth >  8 ) numYDays = numYDays + 31;
if ( numMonth >  9 ) numYDays = numYDays + 30;
if ( numMonth > 10 ) numYDays = numYDays + 31;
if ( numMonth > 11 ) numYDays = numYDays + 30;
numYDays = numYDays + numDate;
// Calculate total number of days for last Sunday
numYDays = numYDays - numDay;
// Integer divide by 7 to get the number of whole weeks
numWeek = ( numYDays - ( numYDays % 7 ) ) / 7;
// Minimum number of days in week 1
numMinDWeek1 = 4;
if ( ( numYDays ) % 7 > numMinDWeek1 ) numWeek = numWeek + 1;
// Show the results
WScript.Echo( "Week=" + numWeek );
// Quit and return week number
WScript.Quit( numWeek );


function Syntax( ) {
	WScript.Echo( );
	WScript.Echo( "Week.js,  Version 1.00 for WSH" );
	WScript.Echo( "Display the current week number." );
	WScript.Echo( );
	WScript.Echo( "Usage:   CScript  WEEK.JS" );
	WScript.Echo( );
	WScript.Echo( "Returns: current week number" );
	WScript.Echo( "         or 255 on syntax errors" );
	WScript.Echo( );
	WScript.Echo( "Assumptions: [1] First day of the week is Sunday" );
	WScript.Echo( "             [2] Week 1 is the first week of the year with at least 4 days" );
	WScript.Echo( "These assumptions can be changed by modifying the script's source code." );
	WScript.Echo( "Read the comments in the source code to find the values to be modified." );
	WScript.Echo( );
	WScript.Echo( "Written by Rob van der Woude" );
	WScript.Echo( "http://www.robvanderwoude.com" );
	WScript.Quit(255);
}

Back to the top of this page... ]

Week.kix

; Week.kix,  Version 2.10
; Display week number using Kix
; Written by Rob van der Woude
; http://www.robvanderwoude.com

; Number of whole weeks this year (Sundays to Saturdays)
; Add 1 to start with week 1 instead of 0
; Thanks to Peter Cranen for this correction
$FULLWEEKS = ( ( @YDAYNO - @WDAYNO ) / 7 ) + 1

; Add 1 week if today isn't Saturday
IF @WDAYNO < 6
	$WEEKS = $FULLWEEKS + 1
ENDIF

; Display result
? "Week " + $WEEKS
?

Back to the top of this page... ]

Week.pl

Modify the first line for use in non-Windows environments.

#! perl

# Week.pl,  Version 2.00
# Display week number using Perl
# Written by Rob van der Woude
# http://www.robvanderwoude.com

# Parse time string
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);

# Calculate number of full weeks this year
$week = int( ( $yday + 1 - $wday ) / 7 );

# Add 1 if today isn't Saturday
if ($wday < 6) {
	$week = $week + 1
}

# Display the result
print "Week $week\n";

Back to the top of this page... ]

Week.ps1

get-Date -uformat %W

Back to the top of this page... ]

Week.rex

/* Week.cmd,  Version 2.00        */
/* Display week number using Rexx */
/* Written by Rob van der Woude   */
/* http://www.robvanderwoude.com  */

/* Translate day of week to number */
select
	when date( "W" ) = "Saturday"  then ndow = 6
	when date( "W" ) = "Friday"    then ndow = 5
	when date( "W" ) = "Thursday"  then ndow = 4
	when date( "W" ) = "Wednesday" then ndow = 3
	when date( "W" ) = "Tuesday"   then ndow = 2
	when date( "W" ) = "Monday"    then ndow = 1
	otherwise ndow = 0
end

/* Calculate number of WHOLE weeks this year (Sundays to Saturdays) */
weeks = ( date( "D" ) - ndow ) % 7

/* Add one more if today isn't Saturday */
if ndow < 6 then weeks = weeks + 1

/* Display the result */
say
say "Week "||weeks

Back to the top of this page... ]

Week.vbs

Replace the vbSunday and vbFirstFourDays constants to modify week 1 definition if necessary.

' Week.vbs,  Version 1.00 for Windows Script Host 2.00
' Display current week number.
'
' Assumptions: [1] First day of the week is Sunday
'              [2] Week 1 is the first week of the year with at least 4 days
' You can modify this by using other VBScript constants (parameters 3 and 4)
' in the DatePart() function.
'
' Written by Rob van der Woude
' http://www.robvanderwoude.com

Wscript.Echo("Week=" & DatePart("ww",Date,vbSunday,vbFirstFourDays))

Back to the top of this page... ]

Yesterday

Yesterday displays — you may have guessed — yesterday's date in various formats.

Yesterday.js

// Yesterday.js,  Version 1.00 for Windows Script Host 2.00
// Display today's and yesterday's date in two formats
//
// Written by Rob van der Woude
// http://www.robvanderwoude.com

// Specify header
strHead1 = "Format:     YYYYMMDD  (DD/MM/YYYY)";
strHead2 = "==================================";

// Create Date object
objToday = new Date();

// Get today's year, month and day
year  = objToday.getFullYear();
month = objToday.getMonth() + 1;
if ( month < 10 )
	{
	month = "0" + month;
	};
day   = objToday.getDate();
if ( day < 10 )
	{
	day = "0" + day;
	};

// Format output for today
strToday = "Today:      " + year + month + day + "  (" + day;
strToday = strToday + "/" + month + "/" + year + ")";

// Get current date in milliseconds since January 1, 1970
today = objToday.valueOf();

// Subtract 1 day
oneDay = 1000 * 24 * 60 * 60;
yesterday = today - oneDay;
objToday.setTime(yesterday);

// Get yesterday's year, month and day
year  = objToday.getFullYear();
month = objToday.getMonth() + 1;
if ( month < 10 )
	{
	month = "0" + month;
	};
day   = objToday.getDate();
if ( day < 10 )
	{
	day = "0" + day;
	};

// Format output for yesterday
strYest = "Yesterday:  " + year + month + day + "  (" + day;
strYest = strYest + "/" + month + "/" + year + ")";

// Display the result
WScript.Echo( strHead1 );
WScript.Echo( strHead2 );
WScript.Echo( strToday );
WScript.Echo( strYest );
RETURN

Back to the top of this page... ]

Yesterday.kix

; Yesterday.kix,  Version 1.00
; Display today's and yesterday's date in two formats
; Written by Rob van der Woude
; http://www.robvanderwoude.com

; Today's date in YYYYMMDD format
$SortDate = ( 10000 * @YEAR ) + ( 100 * @MONTHNO ) + @MDAYNO

; Calculate yesterday's date
$YesterD = @MDAYNO - 1
$YesterM = @MONTHNO
$YesterY = @YEAR

; It gets tricky when today is the first day of the month
IF $YesterD = 0
	GOSUB RollMonth
ENDIF

; Yesterday's date in YYYYMMDD format
$SortYest = ( 10000 * $YesterY ) + ( 100 * $YesterM ) + $YesterD

; Display the results
? "Format:     YYYYMMDD  (DD/MM/YYYY)"
? "=================================="
? "Today:      $SortDate  (@MDAYNO/@MONTHNO/@YEAR)"
? "Yesterday:  $SortYest  ($YesterD/$YesterM/$YesterY)"
?

; Done
EXIT


; * * * * * * * *  Subroutines  * * * * * * * *


; Subroutine to get yesterday's date if today is the first day of the month
:RollMonth
$YesterM = $YesterM - 1
SELECT
	CASE $YesterM = 0 ; Today is January 1st
		$YesterD = 31
		$YesterM = 12
		$YesterY = @YEAR - 1
	CASE $YesterM = 1 ; Today is February 1st
		$YesterD = 30
	CASE $YesterM = 2 ; Today is March 1st
		$YesterD = 28
		GOSUB LeapYear
	CASE $YesterM = 3 ; Today is April 1st
		$YesterD = 31
	CASE $YesterM = 4 ; Today is May 1st
		$YesterD = 30
	CASE $YesterM = 5 ; Today is June 1st
		$YesterD = 31
	CASE $YesterM = 6 ; Today is July 1st
		SET YesterD=30
	CASE $YesterM = 7 ; Today is August 1st
		$YesterD = 31
	CASE $YesterM = 8 ; Today is September 1st
		$YesterD = 31
	CASE $YesterM = 9 ; Today is October 1st
		$YesterD = 30
	CASE $YesterM = 10 ; Today is November 1st
		$YesterD = 31
	CASE $YesterM = 11 ; Today is December 1st
		$YesterD = 30
ENDSELECT
RETURN


; Subroutine to calculate if this year is a leap year
; (I am not sure if the century calculations are right)
:LeapYear
; If the year divisable by 4 then it is a leap year . . .
$LeapYear = ( @YEAR / 4 ) * 4
IF $LeapYear = @YEAR
	$YesterD = 29
ENDIF
; . . . unless it is also divisible by 100 . . .
$LeapYear = ( @YEAR / 100 ) * 100
IF $LeapYear = @YEAR
	$YesterD = 28
ENDIF
; . . . but when it is divisible by 400 it is a leap year again (?)
$LeapYear = ( @YEAR / 400 ) * 400
IF $LeapYear = @YEAR
	$YesterD = 29
ENDIF
RETURN

Back to the top of this page... ]

Yesterday.pl

#! perl

# Yesterday.pl,  Version 1.00
# Display yesterday's "sorted" date (YYYYMMDD)
# Written by Rob van der Woude
# http://www.robvanderwoude.com

# Specify function library
use Time::Local;

# Convert today to epoch seconds
$today = timelocal(localtime);

# Subtract 1 day
@yesterday = localtime($today - (24 * 60 * 60));

# Extract year, month and day, and correct base offsets
$year  = (@yesterday)[5] + 1900;
$month = (@yesterday)[4] + 1;
$day   = (@yesterday)[3];

# Display the result, correctly formatted
printf("\nYesterday = %04d%02d%02d\n", $year, $month, $day);

Back to the top of this page... ]

Yesterday.rex

/* Yesterday.rex,  Version 1.00 for Regina Rexx */
/* Show yesterday's date in YYYYMMDD format     */
/* Written by Rob van der Woude                 */
/* http://www.robvanderwoude.com                */

say
say "Yesterday="||date( "S", date( "B" ) - 1, "B" )

Back to the top of this page... ]

Yesterday.vbs

' Yesterday.vbs,  Version 1.01 for Windows Script Host 2.00
' Display today's and yesterday's date in two formats
'
' Written by Rob van der Woude
' http://www.robvanderwoude.com

' Specify header
strHead    = "Format:     YYYYMMDD  (DD/MM/YYYY)" & Chr(13) & Chr(10) & _
             "==================================" & Chr(13) & Chr(10)


' Get current year
strYear = DatePart("yyyy",Date)

' Get current month, add leading zero if necessary
If DatePart("m",Date) < 10 Then
	strMonth = 0 & DatePart("m",Date)
Else
	strMonth = DatePart("m",Date)
End If

' Get current day, add leading zero if necessary
If DatePart("d",Date) < 10 Then
	strDay = 0 & DatePart("d",Date)
Else
	strDay = DatePart("d",Date)
End If

' Format output for today
strToday = "Today:      " & strYear & strMonth & strDay & "  (" & _
           strDay & "/" & strMonth & "/" & strYear & ")" & Chr(13) & Chr(10)

' Calculate yesterday's date
dtmYesterday = DateAdd("d",-1,Date)

' Get yesterday's year
strYear      = DatePart("yyyy",dtmYesterday)

' Get yesterday's month, add leading zero if necessary
If DatePart("m",dtmYesterday) < 10 Then
	strMonth = 0 & DatePart("m",dtmYesterday)
Else
	strMonth = DatePart("m",dtmYesterday)
End If

' Get yesterday's day, add leading zero if necessary
If DatePart("d",dtmYesterday) < 10 Then
	strDay = 0 & DatePart("d",dtmYesterday)
Else
	strDay = DatePart("d",dtmYesterday)
End If

' Format output for yesterday
strYest = "Yesterday:  " & strYear & strMonth & strDay & "  (" & _
          strDay & "/" & strMonth & "/" & strYear & ")"

' Display the result
Wscript.Echo( strHead & strToday & strYest )

Back to the top of this page... ]

 
JScript KiXtart Perl PowerShell Rexx VBScript
Click to view source
Easter.js
Click to view source
Easter.kix
Click to view source
Easter.pl
  Click to view source
Easter.rex
Click to view source
Easter.vbs
Click to view source
LeapYear.js
Click to view source
LeapYear.kix
Click to view source
LeapYear.pl
Click to view source
LeapYear.ps1
Click to view source
LeapYear.rex
Click to view source
LeapYear.vbs
          Click to view source
SetDate.vbs
Click to view source
SortDate.js
Click to view source
SortDate.kix
Click to view source
SortDate.pl
Click to view source
Today.ps1
Click to view source
SortDate.cmd
Click to view source
SortDate.vbs
Click to view source
SortTime.js
Click to view source
SortTime.kix
Click to view source
SortTime.pl
  Click to view source
SortTime.cmd
Click to view source
SortTime.vbs
Click to view source
Tomorrow.js
Click to view source
Tomorrow.kix
Click to view source
Tomorrow.pl
Click to view source
Today.ps1
Click to view source
Tomorrow.rex
Click to view source
Tomorrow.vbs
  Click to view source
Week.kix
Click to view source
Week.pl
Click to view source
Week.ps1
Click to view source
Week.cmd
Click to view source
Week.vbs
Click to view source
Yesterday.js
Click to view source
Yesterday.kix
Click to view source
Yesterday.pl
Click to view source
Today.ps1
Click to view source
Yesterday.rex
Click to view source
Yesterday.vbs
Click to download source
All Date/Time scripts (ZIPped)

Back to the top of this page...

Back to the top of this page... ]


Examples

The following examples show how to use the non-batch solutions shown above in NT batch files:

Copy all files to a directory with the week number in its name (uses WEEK.VBS shown above):

FOR /F "tokens=*" %%A IN ('CSCRIPT WEEK.VBS //NoLogo') DO SET %%A
XCOPY D:\Source\*.* C:\Backups\Week%WEEK%

or do the same creating WEEK.VBS on the fly:

> "%Temp%.\_WEEK.VBS" ECHO Wscript.Echo("Week=" ˆ& DatePart("ww",Date,vbSunday,vbFirstFourDays))
FOR /F "tokens=*" %%A IN ('CSCRIPT "%Temp%.\_WEEK.VBS" //NoLogo') DO SET %%A
DEL "%Temp%.\_WEEK.VBS"
XCOPY D:\Source\*.* C:\Backups\Week%WEEK%

Copy all files to a directory named after the sorted current date and time (uses SORTDATE.PL and SORTTIME.PL shown above):

FOR /F "tokens=3" %%A IN ('SORTDATE.PL') DO SET SORTDATE=%%A
FOR /F "tokens=3" %%A IN ('SORTTIME.PL') DO SET SORTTIME=%%A
XCOPY D:\Source\*.* C:\Backups\%SORTDATE%%SORTTIME%\*.* /I /S

Copy all files to a directory named after today's sorted date (using SETDATE.VBS, scheduled at midnight and at system startup):

XCOPY D:\Source\*.* C:\Backups\%Date.Year%%Date.Month%%Date.Day%\*.* /I /S

Copy all files to a directory named after yesterday's date (uses YESTERDAY.PL shown above, and assumes .PL has been added to PATHEXT):

FOR /F "tokens=2 delims== " %%A IN ('YESTERDAY.PL') DO SET Yesterday=%%A
XCOPY D:\Source\*.* C:\Backups\%Yesterday%\ /I /S

Back to the top of this page... ]

Back to the top of this page... ]


page last modified: 2022-02-20; loaded in 0.0035 seconds