Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Negative values in C# DateTime

Tags:

c#

datetime

Is there a way to set negative values when calling the DateTime constructor.

like this:

DateTime date = new DateTime(2011, 2, -1);

would be the same as:

DateTime date = new DateTime(2011, 1, 31);

I know this works in other languages, but the problem is that C# .net throws an exception when i'm doing this. Any ideas on how to do this in C# .net?

like image 572
Andreas Avatar asked Jan 24 '11 11:01

Andreas


People also ask

Can you use negative numbers in C?

C and C++ are unusual amongst languages nowadays in making a distinction between signed and unsigned integers. An int is signed by default, meaning it can represent both positive and negative values.

Can float store negative values in C?

Floating point numbers can be positive or negative.

What is the value of negative?

A real quantity having a value less than zero ( < 0 ) is said to be negative. Negative numbers are denoted with a minus sign preceding the corresponding positive number, i.e., -2, -100.

Can double take negative values in C?

On all machines, variables of the float, double, and long double data types can store positive or negative numbers.


1 Answers

Use

DateTime date = new DateTime(2011, 2,1).AddDays(-1);
like image 73
Jens Avatar answered Oct 20 '22 04:10

Jens