DateTime.MinValue.Year gives 2001 when parsed?

S

shapper

Hello,

I tried the following:

String dts = String.Format("{0}-{1}-{2}", DateTime.MinValue.Day,
DateTime.MinValue.Month, DateTime.MinValue.Year);

DateTime dt = DateTime.Parse(dts);

dts becomes "1-1-1" but dt becomes "01-01-2001".

Shouldn't the minimum year be "0001" which is what I get when i do
DateTime dt = new DateTime();

Thanks,

Miguel
 
A

Arne Vajhøj

I tried the following:

String dts = String.Format("{0}-{1}-{2}", DateTime.MinValue.Day,
DateTime.MinValue.Month, DateTime.MinValue.Year);

DateTime dt = DateTime.Parse(dts);

dts becomes "1-1-1" but dt becomes "01-01-2001".

Shouldn't the minimum year be "0001" which is what I get when i do
DateTime dt = new DateTime();

The following should illustrate problem and solution:

using System;

namespace E
{
public class Program
{
public static void Main(string[] args)
{
DateTime dt0 = new DateTime();
Console.WriteLine(dt0);
String dt1s = String.Format("{0}-{1}-{2}",
DateTime.MinValue.Day, DateTime.MinValue.Month, DateTime.MinValue.Year);
Console.WriteLine(dt1s);
DateTime dt1 = DateTime.Parse(dt1s);
Console.WriteLine(dt1);
String dt2s = String.Format("{0:00}-{1:00}-{2:0000}",
DateTime.MinValue.Day, DateTime.MinValue.Month, DateTime.MinValue.Year);
Console.WriteLine(dt2s);
DateTime dt2 = DateTime.Parse(dt2s);
Console.WriteLine(dt2);
String dt3s = String.Format("{0}-{1}-{2}",
DateTime.MinValue.Day, DateTime.MinValue.Month, DateTime.MinValue.Year);
Console.WriteLine(dt3s);
DateTime dt3 = DateTime.ParseExact(dt3s, "d-M-y", null);
Console.WriteLine(dt3);
String dt4s = String.Format("{0:00}-{1:00}-{2:0000}",
DateTime.MinValue.Day, DateTime.MinValue.Month, DateTime.MinValue.Year);
Console.WriteLine(dt4s);
DateTime dt4 = DateTime.ParseExact(dt4s, "dd-MM-yyyy", null);
Console.WriteLine(dt4);
Console.ReadKey();
}
}
}

Arne
 
A

Arne Vajhøj

The minimum year _is_ "0001" (and "1" and "01" and however else you want
to write the integer 1 as decimal text).

But, just because you generated the string you're parsing using
DateTime.MinValue as the input, that doesn't mean the DateTime struct
has any way to know that the "1" in your string means the year 1.

The DateTime parsing is a fairly complex set of rules intended to
produce the most likely intended results for a wide variety of input.
You fed it "1" and it quite reasonably assumed you meant the nearby year
of 2001, rather than the year that almost no computer program ever has
to worry about.

If you want the string input to be interpreted differently, you need to
provide an exact format string to the ParseExact() method instead.
Alternatively, just do the parsing yourself.

ParseExact has the same problem/feature regarding single digit years -
the only improvement is that it is possible to get an exception if
a 4 digit year is expected.

For that old dates 4 digit years are needed.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top