get yesterday's date in typescript

To get yesterday's date in TypeScript, you can use JavaScript's built-in Date object and Moment.js library.

Here's an example code snippet using Moment.js:

index.ts
import * as moment from 'moment-timezone';

const yesterday = moment().subtract(1, 'days').tz('UTC');
console.log(yesterday.format());
135 chars
5 lines

In this code, we import the Moment.js library and use the moment() function to get the current date and time in the UTC timezone. We then subtract 1 day from this moment using the subtract() function and specify the unit of time to subtract (in this case, 'days'). Finally, we format the resulting yesterday moment object using the format() function and print it to the console.

If you don't want to use Moment.js, you can use plain JavaScript to get yesterday's date as follows:

index.ts
const yesterday = new Date();
yesterday.setDate(yesterday.getDate() - 1);
console.log(yesterday.toISOString());
112 chars
4 lines

In this code, we create a new Date object representing the current date and time. We then subtract 1 day from this date using setDate() and getDate() functions. Finally, we convert the yesterday date object to an ISO string format using the toISOString() function and print it to the console.

gistlibby LogSnag