How to Subtract Dates in TypeScript (With Examples)

TypeScript subtract dates: Command Prompt output breaking a date difference into days, hours and minutes

Subtracting dates in TypeScript comes down to one move: call getTime() on both Date objects and subtract the numbers. The result is the gap in milliseconds, which you divide down to seconds, hours or days. Writing end – start directly won’t compile in TypeScript: In this tutorial, we’ll subtract dates in TypeScript step by step. … Read more >>

How to Add Seconds to a Date in TypeScript

TypeScript add seconds to date: Command Prompt output of an addSeconds helper using getTime

The cleanest way to add seconds to a date in TypeScript is to read the timestamp with getTime(), add the seconds times 1000, and wrap the sum in a new Date. Your original date isn’t touched: I ran every snippet here with TypeScript 7.0.2 and Node.js v22.22.2 in strict mode, so the printed results are … Read more >>

How to Format Numbers as Two Digits in TypeScript

Format numbers as two digits in TypeScript: Command Prompt output of Intl.NumberFormat with minimumIntegerDigits

The quickest way to format numbers as two digits in TypeScript is String(n).padStart(2, “0”), which turns 7 into “07” and leaves 42 alone. If you’d rather use a built-in formatter, set minimumIntegerDigits: 2 in Intl.NumberFormat: Nothing below is pseudo-code: I built each file under TypeScript 7.0.2 and Node.js v22.22.2 with –strict and copied the console … 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 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 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 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 Calculate Days Between Two Dates in TypeScript

Days between dates in TypeScript: VS Code showing a day count across daylight saving time fixed with Date.UTC

Counting days between dates in TypeScript takes one line: subtract the two getTime() values and divide by 86,400,000, the number of milliseconds in a day. Wrap it in Math.round() so a daylight saving shift can’t leave you with 13.96 days: In this tutorial, we’ll count the days between dates in TypeScript the safe way, from … Read more >>

How to Avoid Duplicate Enum Values in TypeScript

Avoid Duplicate Enum Values in TypeScript

Avoiding duplicate enum values in TypeScript is important to maintain clarity and avoid unexpected behavior when using enums. Here’s a realistic scenario showing how duplicate values can cause problems and how to avoid them. In this tutorial, we will learn how to avoid duplicate Enum values in TypeScript, how duplicate enum values can lead to … Read more >>

How to Check if an Enum Contains a Value in TypeScript

Enum Contains a Value in TypeScript

Suppose you’re building an admin panel for a web app where users can have one of several roles, such as admin, editor, or viewer. You receive the user role from a backend API as a string, and you want to check whether this string is a valid role according to your enum before using it … Read more >>