typestar

TimeSpan in C#

Durations that add, compare and scale like numbers.

var build = TimeSpan.FromSeconds(212);
var tests = TimeSpan.FromMinutes(3.5);

var total = build + tests;
Console.WriteLine(total);
Console.WriteLine(total.TotalMinutes);

// spans compare and scale like the durations they are
Console.WriteLine(build > tests);
Console.WriteLine(build * 2);
Console.WriteLine($"{build:mm\\:ss}");

How it works

  1. FromSeconds and FromMinutes build spans readably.
  2. Spans support +, > and * directly.
  3. The mm\\:ss format string prints a compact clock.

Keywords and builtins used here

The run, in numbers

Lines
11
Characters to type
329
Tokens
69
Three-star pace
95 tpm

At the three-star pace of 95 tokens a minute, this run takes about 44 seconds.

Type this snippet

Step 2 of 3 in Dates & times, step 8 of 17 in The .NET library.

← Previous Next →