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.ts135 chars5 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.ts112 chars4 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