How to Remove Time from a Date in TypeScript

Remove time from date in TypeScript: Command Prompt output of setHours on a copied Date

To remove time from date in TypeScript, copy the Date and call setHours(0, 0, 0, 0) on the copy. That sets the clock to local midnight and keeps a real Date you can compare. If you only need text, use toLocaleDateString(“en-CA”) for YYYY-MM-DD: I ran every snippet on TypeScript 7.0.2 and Node.js v22.22.2 in strict … Read more >>

How to Get the First and Last Day of the Week in TypeScript

Get first and last day of week in TypeScript: Command Prompt output for Sunday-start and Monday-start weeks

To get the first and last day of week in TypeScript, copy the date, subtract getDay() days with setDate() to reach Sunday, then add 6 days to reach Saturday: My test box runs on Chicago time, so the DST dates below are real US ones. I built each file with TypeScript 7.0.2 and Node.js v22.22.2 … Read more >>

How to Check if a Date Is Less Than Today in TypeScript

Check date less than today in TypeScript: Command Prompt output of an isBeforeToday function

Here’s how to check if a date is less than today in TypeScript: set a copy of today to midnight, then compare with <. Anything earlier than that midnight falls on a previous day, so a time earlier today won’t count: Every snippet below ran on TypeScript 7.0.2 and Node.js v22.22.2 with –strict, on a … Read more >>

How to Format Date with Timezone in TypeScript

Format date with timezone in TypeScript: Command Prompt output of one instant in five US time zones

You can format a date with a timezone in TypeScript by passing an IANA zone name, like America/New_York, as the timeZone option of Intl.DateTimeFormat or toLocaleString(). The Date itself never changes; only the text you print does: I tested the code below with TypeScript 7.0.2 and Node.js v22.22.2 in strict mode. My test machine’s clock … Read more >>

How to Get Week Number from Date in TypeScript (ISO 8601)

Get week number from date in TypeScript: Command Prompt output of ISO week-year edge cases

You can get the week number from a date in TypeScript with a short function: move the date to the Thursday of its week, then count 7-day blocks from January 1. That gives the ISO 8601 week, where weeks start on Monday: JavaScript’s Date has no built-in week method, so I wrote and compiled every … Read more >>

How to Compare Dates in TypeScript?

Custom Date Comparison Types in TypeScript

I was working on a TypeScript project where I had to compare two dates to check which one was earlier. First, I attempted to do it by comparing two numbers, but it didn’t work as expected. It has issues with time zones, different formats, and even errors with the time part of the date. Later, … Read more >>

Format Date as MM/DD/YYYY in TypeScript

Convert Date format to MM-dd-yyyy in Typescript

While working on a TypeScript project, I needed to display dates in the American format (MM/DD/YYYY) instead of the default ISO format (YYYY-MM-DD). TypeScript’s built-in Date object and the toLocaleString() method make this easy and no external libraries required. In this blog, I’ll show you how to format date as MM/DD/YYYY in TypeScript using toLocaleString(), … Read more >>

Subtract Days From a Date in TypeScript

Subtract days from date in Typescript using library

Recently, while working on a TypeScript project, I needed to find a date a few days before an important event, like when to start preparing or send reminders. If I had changed the dates directly, it could have caused issues in the code. So, I created a reusable function that subtracts days using TypeScript’s Date … Read more >>

How to Add Days to a Date in TypeScript (setDate and DST)

TypeScript add days to date: Command Prompt comparing setDate with adding 86400000 ms across daylight saving time

Copy the Date, then call setDate(getDate() + days) on the copy: that’s how you add days to a date in TypeScript. JavaScript rolls the month and year over for you, and negative numbers go back in time: Every result here came from TypeScript 7.0.2 and Node.js v22.22.2 in strict mode, with the time zone set … Read more >>

Sort Arrays by Date in TypeScript

Sort Array Dates in Descending Order in TypeScript

When I was working on a TypeScript project recently, I had to display a list of events sorted by their dates. I used .sort() to arrange them, but it didn’t work as expected because the dates were a mix of strings and Date objects. To fix this, I converted all the date values to Date … Read more >>