Does not allow subtraction between Dates


var millis = new Date() - new Date(1970, 0, 1);

generates the following error:

Operator '-' cannot be applied to types 'Date' and 'Date'

I'm not sure the ES specs allow it but the JS engines I've used support this (may be a consequence of Date having a valueOf method).
Closed Jul 28, 2014 at 11:18 PM by jonturner
As part of our move to GitHub, we're closing our CodePlex suggestions and asking that people move them to the GitHub issue tracker for further discussion. Some feature requests may already be active on GitHub, so please make sure to look for an existing issue before filing a new one.

You can find our GitHub issue tracker here:


bjouhier wrote Oct 7, 2012 at 9:41 PM

From http://ecma-international.org/ecma-262/5.1/#sec-11.6.2 and http://ecma-international.org/ecma-262/5.1/#sec- subtraction should work on Dates (and on any object with a valueOf method that returns a number).

rwaldron wrote Oct 7, 2012 at 9:53 PM

Unfortunately, the reason subtraction works on Dates in JavaScript is the exact thing that TypeScript exists to prevent. In short, you can't have your cake and coerce it, too. JavaScript allows math operations on Date objects because the operator itself coerces the LHS and RHS values into types it can work with—in this case numbers.

A work around:

Date.now() - +(new Date(1970, 0, 1));

Which will amusing compile to...

Date.now() - +(new Date(1970, 0, 1));

bjouhier wrote Oct 8, 2012 at 10:01 PM

Valid point.

This a rather minor annoyance anyway and the + prefix workaround is good enough for me (I had fixed it with .getTime() on both operands, which I found a bit heavy).


jonturner wrote Oct 8, 2012 at 10:06 PM

TypeScript doesn't support these kind of coercions because we don't know generally where all the coercions should be applied.

** Closed by jonturner 10/08/2012 2:06PM

jonturner wrote Oct 10, 2012 at 4:21 PM

Reopening for further comment

staxmanade wrote Aug 31, 2013 at 6:01 AM

For anyone starting to convert existing JavaScript into a TypeSript project this will be one that others might often bump into.

It would make it easier on the conversion if the compiler could support it directly. But if the decision is to not, then I think at minimum a WIKI page of common conversion issues and work-arounds would be ideal to put together (and this one on there).

GeoffArmstrong wrote May 4, 2014 at 12:17 AM

I agree, this is fine to leave out. Requiring +date is fine.

wshager wrote Jan 17 at 4:41 PM

A solution could be to create another type that CAN be coerced, because it relies on a normalized date / dateTime object, and abstract from that. Take for example XMLSchema xs:date / xs:dateTime & friends, where it's absolutely legal to subtract dates. I'm not saying the W3C has all the answers (oh wait, it does). Running into exceptions makes me regret using TS, because it really hurts productivity.