Skip to main content

TimeSpan in C#

 

TimeSpan in C#

The struct TimeSpan in C# represents a time interval that is difference between two times measured in number of days, hours, minutes, and seconds. The TimeSpan is used to compare two C# DateTime objects to find the difference between two dates.

 Creating TimeSpan

 TimeSpan struct has the following overloaded forms:

 

TimeSpan Method

The following code snippet crates a TimeSpan from days, hours, and minutes.

C# TimeSpan


The common method of creating a TimeSpan is finding the difference between two DateTime objects, The following code snippet is example of getting the interval between two DateTime objects.

TimeSpan Properties

 C# TimeSpan class properties are Days, Hours, Minutes, Seconds, Milliseconds, and Ticks that returns days, hours, minutes, seconds, and milliseconds in a TimeSpan object. The TotalDays, TotalHours, TotalMinutes, TotalSeconds, and TotalMilliseconds properies return the totals of them on an object.

 

The following code snippet gets a TimeSpan between two dates and reads these properties.

TimeSpan Property


TimeSpan Methods

 TimeSpan class provides FromDays, FromHours, FromMinutes, FromSeconds, and FromMilliseconds methods to create TimeSpan objects from days, hours, minutes, seconds, and milliseconds respectively.

 

The following code snippet adds TimeSpan objects using the Fromxxx methods.

Methods


The Add, Subtract, Multiply, Divide, and Negate methods to adds, subtract, divide, multiply, and negate

TimeSpan objects.

 The following code snippet is an example of how to add and subtract TimeSpan objects.

Timespan obj


The Parse, ParseExact, TryParse, TryParseExact, and TryFormat methods are used to parse and format TimeSpan objects into strings and vice versa.

Comments

Popular posts from this blog

The String.Join Method in C# Explained

The String.Join Method in C#   The string.Join concatenates the elements of a specified array or the members of a collection, using the specified separator between each element or member. Overloads of string.Join Method Description Join(Char, Object[]) Concatenates the string representations of an array of objects, using the specified separator between each member. Join(Char, String[]) Concatenates an array of strings, using the specified separator between each member. Join(String, IEnumerable<String>) Concatenates the members of a constructed IEnumerable<T> collection of type String, using the specified separator between each member. Join(String, Object[]) Concatenates the elements of an object array, using the specified separator between each element. Join(String, String[]) Concatenates all the elements of a string array, usi...

Models in ASP.NET MVC

Models in ASP.NET MVC Explained A model is a class that contains the business logic of your application. It also used for accessing data from the database. The model class does not handle directly input from the browser. In MVC, it is the Controller that handle input from the browser directly and process the request by receiving data from the model and pass it back to view as response. It does not contain any HTML code either. It is a best practice but not mandatory for developers to not have any communications with the view directly, models should only contain a POCO ( Plain Old CLR Objects ) classes. All processing logic and communication with the view should be handled by another layer called Viewmodels. Models are also refers as objects that are used to implement conceptual logic for the application. A controller interacts with the model, access the data, perform the logic and pass that data to the view. Note that it is not mandatory, but it is a good programming...

Classes in C# Explained

C# Class Explained A class is nothing but an encapsulation of properties and methods that are used to represent a real-time entity, as explained by Guru99 . For instance, if you want to work with Guest’s data as in our previous DataDriven Web application . The properties of the Guest would be the Id, GuestName, Address, Phone number etc of the Guest. The methods would include the entry and modification of Guest data. All of these operations can be represented as a class in C# as shown below. using System; namespace CsharpnaijaClassTutorial {     public class Guest     {         public int Id { get ; set ; }         public string GuestName { get ; set ; }         public string Address { get ; set ; }         public string WhomToSee { get ; set ; }     ...