Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a Timespan of 1 year?

Tags:

c#

timespan

I want to get a Timespan structure which represent a year in C#.

like image 934
Florian Avatar asked Dec 02 '11 11:12

Florian


People also ask

What is TimeSpan time?

A TimeSpan object represents a time interval (duration of time or elapsed time) that is measured as a positive or negative number of days, hours, minutes, seconds, and fractions of a second. The TimeSpan structure can also be used to represent the time of day, but only if the time is unrelated to a particular date.

How do I add time to TimeSpan?

Add() Method in C# This method is used to a get new TimeSpan object whose value is the sum of the specified TimeSpan object and this instance. Syntax public TimeSpan Add (TimeSpan t);

How do I convert DateTime to TimeSpan?

To convert a DateTime to a TimeSpan you should choose a base date/time - e.g. midnight of January 1st, 2000, and subtract it from your DateTime value (and add it when you want to convert back to DateTime ). If you simply want to convert a DateTime to a number you can use the Ticks property.


1 Answers

The tricky thing is that what a year is, depends on where it starts.

You can do

DateTime now = DateTime.Now; TimeSpan span = now.AddYears(1) - now; 

This would give you the 1 year timespan from the current moment to one year later

like image 137
sehe Avatar answered Sep 21 '22 12:09

sehe