Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C#

System.TimeSpan: practical reference

System.TimeSpan: practical reference: TimeSpan represents a duration as a number of ticks, not a date or time zone.

Version: .NET API; verified against .NET 10 reference surface

Syntax

System.TimeSpan /* select a documented member or constructor */

TimeSpan represents a duration as a number of ticks, not a date or time zone.

Use it for timeouts and time differences within its supported range.

Hours and Minutes are components; use TotalHours or TotalMinutes for the full value.

Operations beyond the range throw OverflowException; parsing depends on format and culture.

The complete program actually uses System.TimeSpan; its exact expected stdout is 1.5.

Examples

using System;
using System.Buffers;
using System.Collections.Concurrent;
using System.Collections.Frozen;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Channels;
using System.Threading.Tasks;
using Number = System.Int32;

var duration = TimeSpan.FromMinutes(90);
Console.WriteLine(duration.TotalHours.ToString("0.0", CultureInfo.InvariantCulture));

Result

1.5

The complete program actually uses `System.TimeSpan`; its exact expected stdout is `1.5`.

Sources