JavaScript

roger's picture

Subtracting dates in Javascript

Here's how to find the time 30 minutes ago:

var THIRTY_MINUTES_IN_MS = 30 * 60 * 1000;

var now = new Date();
var nowMs = now.getTime();
    
var thenMs = nowMs - THIRTY_MINUTES_IN_MS;
var then = new Date(thenMs);

The secret is that Date.getTime returns the number of milliseconds since the epoch, and that the Date constructor accepts these millisecond values.

roger's picture

Calling C# from JScript

It's possible, through the magic of COM interop, to call C# code from JScript or VBScript. Here's an example of how to do it from JScript.

roger's picture

Implementing For Each in JScript

function forEach(enumerable, delegate)
{
    for (var enumerator = new Enumerator(enumerable); !enumerator.atEnd(); enumerator.moveNext())
    {
        delegate(enumerator.item());
    }
}

Used like this:

forEach(employees,
        function(employee)
        {
            WScript.Echo(employee.Salary);
        }
    );

Syndicate content