Duration Duration Duration Duration Struct


Represents the duration of time that a Timeline is active, or more generally represents a duration of time that also supports two special values Automatic and Forever.

public : struct Duration
struct winrt::Windows::UI::Xaml::Duration
public struct Duration
Public Structure Duration
<object property="[days.]hours:minutes:seconds[.fractionalSeconds]"/>
<object property="Automatic" .../>
<object property="Forever" .../>

Windows 10 requirements

Device family
Windows 10 (introduced v10.0.10240.0)
API contract
Windows.Foundation.UniversalApiContract (introduced v1)


A Duration value is used for these properties:

The most common way to use a Duration value in the Windows Runtime is to set it using a XAML attribute. When you set a value in XAML, you're supplying a string, and the string is parsed using the hours:minutes:seconds string format and its variants as is described in the XAML Syntax sections near the beginning of this reference topic.

Specifying a Duration using a string that resembles an integer, without any literal characters used in the hours:minutes:seconds string format such as : or . will result in a Duration of that number of days! This is seldom the intended result. Usually you specify animation durations in seconds. As such, the Duration string must include preceding 0 values for hours and minutes, with literal : characters as separators between hours and minutes, and between minutes and seconds. For example, to specify a Duration of five seconds, the correct string for either or a XAML attribute value is "0:0:5" ("0:0:05" is equivalent).

If you're using a Duration in code, a Duration uses a definition of time that is also used by the TimeSpan structure. The TimeSpan structure is represented by System.TimeSpan if you are programming using C# or Microsoft Visual Basic, or Windows.Foundation.TimeSpan if you are programming using C++.

  • The C# or Microsoft Visual Basic System.TimeSpan has a Parse method that use hours:minutes:seconds string format. If you need to create a Duration value in code you can call the Duration constructor and provide the System.TimeSpan argument by calling TimeSpan.Parse with an hours:minutes:seconds string. Always use the "en-us" culture for parsing this string, because that's how XAML interprets the string format, and you shouldn't be using culture-specific inputs for animating timings anyways.
  • The C++ Windows.Foundation.TimeSpan doesn't support a way to create it in an hours:minutes:seconds string format. You'll have to use DurationHelper.FromTimeSpan, and do the conversion yourself for how hours:minutes:seconds converts to the C++ Windows.Foundation.TimeSpan data value, which is a value in milliseconds.

Notes on XAML syntax

In the grammar shown in the XAML attribute usage, [] indicates optional values, the [] (square brackets) are not literals. The : (colon) and . (period) characters are both literals, and delimit the hⓂ️s string form of a common time span, or the optional days and fractionalSeconds values.

Use the literal strings "Automatic" and "Forever" as XAML attribute values if you want a Duration that has behavior as documented by Duration.Automatic and Duration.Forever.

Duration does not support an object element syntax, and you cannot declare a Duration as a shareable item in a ResourceDictionary.

Projection and members of Duration

If you are using a Microsoft .NET language (C# or Microsoft Visual Basic), or in Visual C++ component extensions (C++/CX), then Duration has non-data members available, and its data members are exposed as read-write properties, not fields. Duration exposes several operators, including comparison operators.

For Microsoft .NET, Duration exposes TimeSpan.Parse for its TimeSpan, Implicit and UnaryPlus operators, and Add and Subtract methods. These aren't available from the structure in Visual C++ component extensions (C++/CX) but you can use equivalent DurationHelper methods for some of these.

If you are programming with C++ using the Windows Runtime Template Library (WRL), then only the data member fields exist as members of Duration, and you cannot use the utility methods or properties listed in the members table. WRL code can access similar utility methods that exist on the DurationHelper class. For example, you can call DurationHelper.Compare to compare two C++ Duration values. For more info, see DurationHelper.

Automatic and Forever

Automatic and Forever are values that hold special meaning for a Duration property value. For Microsoft .NET, these are represented by the static properties Automatic and Forever.

The Automatic value applied in either XAML or code results in different behavior on a Storyboard as opposed to an animation. For Storyboard, the Automatic value sets the effective time span to be equal to the end time of its longest-running child animation, such that no clipping occurs for any child animation. For animations, the Automatic value results in the behavior whereby the animation runs with a time span of 1 second (0:0:1). This behavior is seldom desirable as a final result, but it enables you to see the running animation during testing, before you have established a final time span.

Using Forever for an animation is a deprecated usage, and is seldom used. This results in an animation that never advances from its starting value, no matter what values were provided for From/To, key frames, and so on. If you want an animation to repeat continuously, use RepeatBehavior="Forever", not Duration="Forever".


TimeSpan TimeSpan TimeSpan TimeSpan

The TimeSpan value component.

Type Type Type Type

The type as a member of the enumeration.

See also