0

Using for example the following code:

const buildDateTime = new Date(xy);

I would need to replace xy by the actual time my code was last built. The result would be to print:

The software was built at {buildDateTime}.

It is supported in C++, so I hope there is a way to achieve this in TypeScript as well.

Kyll
  • 7,036
  • 7
  • 41
  • 64
Tomas Kubes
  • 23,880
  • 18
  • 111
  • 148
  • I mean replace by fixed date. replace by date of compilation. Like the C++example I provided. – Tomas Kubes Sep 12 '19 at 14:08
  • I tidied your question. At 14k, you should at least know how to properly format your code =p Please check the [help] for more information. – Kyll Sep 12 '19 at 14:11

1 Answers1

3

I'm fairly confident that this isn't directly supported by TypeScript: there's no way to describe custom generated output in the way that you can with a language like C++.

Instead, I'd suggest generating a buildTime.txt file, and using that. E.g. in your package.json:

"scripts": {
  "build": "date +%s > ./build/buildTime.txt && tsc"
}

and in your code, assuming this is Node.js:

const fs = require('fs');
fs.readFile('./buildTime.txt', (buildTime) => {
  // Use your build time however you'd like
});
Tim Perry
  • 11,766
  • 1
  • 57
  • 85