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 Sort by Enum in TypeScript (Numeric and String Enums)

Sort by enum in TypeScript: Command Prompt output of orders sorted alphabetically and by a Record order map

To sort by enum in TypeScript, compare the enum members inside sort(). A numeric enum is already a number, so a.priority – b.priority works. A string enum needs a rank map typed as Record<Enum, number>: Every snippet here ran in strict mode on TypeScript 7.0.2 and Node.js v22.22.2. The output blocks are pasted straight from … Read more >>

How to Get the First Element from a TypeScript Dictionary

Get first element from a TypeScript dictionary: Command Prompt output of a Map's first entry with iterator next()

To get the first element from a TypeScript dictionary, read index 0 of Object.entries() for a plain object or Record, and call .entries().next().value for a Map. Both give back undefined when the dictionary is empty: I ran all five examples with TypeScript 7.0.2 and Node.js v22.22.2 (strict mode on), so the outputs you see are … Read more >>

How to Split a String with Multiple Separators in TypeScript

Split a string with multiple separators in TypeScript: Command Prompt output of split with a character class

You split a string with multiple separators in TypeScript by passing a regular expression to split(). Put single-character separators in a character class like /[,;|]/, and you’ll get back a string[] with one piece per value: All examples were compiled in strict mode with TypeScript 7.0.2 and Node.js v22.22.2, and I pasted the console output … Read more >>

How to Round to 2 Decimals in TypeScript (Without the 1.005 Bug)

Round to 2 decimals in TypeScript: Command Prompt output of a roundTo helper fixing 1.005 and 4.015

The quickest way to round to 2 decimals in TypeScript is Math.round(value * 100) / 100, which returns a number. It breaks on halfway values like 1.005, though, so when every cent matters, shift the decimal point with exponent notation instead: I ran every snippet through TypeScript 7.0.2 and Node.js v22.22.2 in strict mode, so … Read more >>

How to Convert a TypeScript Enum to a Number (With Examples)

Convert TypeScript enum to number: Command Prompt output of a numeric enum read as a number

The simplest way to convert TypeScript enum to number values is to read the member itself: a numeric enum member already is a number at runtime, so const n: number = OrderStatus.Shipped works with no cast. String enums are different, and need a lookup map: Nothing below is hand-typed output: I ran each file through … Read more >>

How to Format Numbers with Commas in TypeScript

Format numbers with commas in TypeScript: Command Prompt output of Intl.NumberFormat with thousands separators

The quickest way to format numbers with commas in TypeScript is value.toLocaleString(“en-US”). It returns a string with a comma between every group of three digits, so 1284567.5 becomes “1,284,567.5”: Nothing below is hand-typed output: I pasted each result from TypeScript 7.0.2 and Node.js v22.22.2, compiling with –strict turned on. Below, I’ll cover toLocaleString, a reusable … Read more >>

How to Convert a String to an Enum in TypeScript

Convert string to enum in TypeScript: Command Prompt output of a type guard parsing order statuses

To convert a string to an enum in TypeScript, check the string against the enum’s values with a type guard, then return the enum member or undefined. A plain as cast checks nothing: My test setup for this post: TypeScript 7.0.2 and Node.js v22.22.2, strict mode on, and nothing in the outputs below is edited … Read more >>