Simple problem, I have a two digit year input which i want to be interpreted as a four digit year so i assumed the following date time constructor would do the trick:
System.Globalization.Calendar dteCal=System.Globalization.CultureInfo.InvariantCulture.Calendar;
DateTime fileDate = new DateTime(15, 2,25 , dteCal);
This is because dteCal.TwoDigitYearMax=2029 so i would expect fileDate to be 25/2/2015 (UK date). However, it is 25/02/0015.
Now I have got round this by int fileYear=dteCal.ToFourDigitYear(int.Parse(lstItems[0].Substring(6,2))) and using the constructor without the calendar object but surely there must be a way to create a DateTime object when you only have a two digit year?
I guess you need to use the Calendar to get the 4 digit Year from 2 digit Year. If you want the other way you have to write a function and manually do that how the .NET Framework has done internally for Calendar.
System.Globalization.Calendar dteCal = System.Globalization.CultureInfo.InvariantCulture.Calendar;
int year = dteCal.ToFourDigitYear(15);
DateTime fileDate = new DateTime(year, 2, 25);
Since DateTime objects can technically support dates as low as the year 15 (e.g. 0015), you aren't going to be able to use this particular constructor by passing in 15 (as .NET will associate that to the year 15).
You may want to consider actually parsing the Date instead in the format that you are expecting :
// Define your day, month, year here
int day = 25;
int month = 2;
int year = 15;
// Parse your date as expected
DateTime fileDate = DateTime.ParseExact(String.Format("{0:00}/{1:00}/{2:00}",day,month,year),"dd/MM/yy", null);
Or you could make this into your own function as follows :
public DateTime DateTimeWithTwoDigitYear(int year, int month, int day)
{
DateTime dt = DateTime.MinValue;
DateTime.TryParseExact(String.Format("{0:00}/{1:00}/{2:00}",day,month,year),"dd/MM/yy", DateTimeStyles.None, null, out dt);
return dt;
}
DateTime objects do not have any formatting on their own, so whenever you have a particular format that you want to use to output a DateTime object, you'll need to explicitly define the format.
B-) Gerry Lowry, Chief Training Architect, Paradigm Mentors Learning never ends... +1 705-999-9195 wasaga beach, ontario canada TIMTOWTDI =.there is more than one way to do it
DateTime.TryParseExact(String.Format("{0:00}/{1:00}/{2:00}",day,month,year),"dd/MM/yy", DateTimeStyles.None, null, out dt);
should be:
DateTime.TryParseExact(String.Format("{0:00}/{1:00}/{2:00}", day, month, year), "dd/MM/yy",null, DateTimeStyles.None, out dt);
B-) Gerry Lowry, Chief Training Architect, Paradigm Mentors Learning never ends... +1 705-999-9195 wasaga beach, ontario canada TIMTOWTDI =.there is more than one way to do it
Member
19 Points
89 Posts
Misunderstanding this DateTime constructorSystem.Globalization.Calendar dteCal=System.Globalizati...
Feb 26, 2015 09:11 AM|mmacneill123|LINK
Hi folks
Simple problem, I have a two digit year input which i want to be interpreted as a four digit year so i assumed the following date time constructor would do the trick:
This is because dteCal.TwoDigitYearMax=2029 so i would expect fileDate to be 25/2/2015 (UK date). However, it is 25/02/0015.
Now I have got round this by int fileYear=dteCal.ToFourDigitYear(int.Parse(lstItems[0].Substring(6,2))) and using the constructor without the calendar object but surely there must be a way to create a DateTime object when you only have a two digit year?
datetime
Star
9021 Points
2415 Posts
Re: Misunderstanding this DateTime constructorSystem.Globalization.Calendar dteCal=System.Globali...
Feb 26, 2015 09:27 AM|Lokesh B R|LINK
Hi,
I guess you need to use the Calendar to get the 4 digit Year from 2 digit Year. If you want the other way you have to write a function and manually do that how the .NET Framework has done internally for Calendar.
https://msdn.microsoft.com/en-us/library/system.globalization.calendar.tofourdigityear%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396
datetime
All-Star
114593 Points
18503 Posts
MVP
Re: Misunderstanding this DateTime constructorSystem.Globalization.Calendar dteCal=System.Globali...
Feb 26, 2015 09:35 AM|Rion Williams|LINK
Since DateTime objects can technically support dates as low as the year 15 (e.g. 0015), you aren't going to be able to use this particular constructor by passing in 15 (as .NET will associate that to the year 15).
You may want to consider actually parsing the Date instead in the format that you are expecting :
Or you could make this into your own function as follows :
You can see a working example of this here.
DateTime objects do not have any formatting on their own, so whenever you have a particular format that you want to use to output a DateTime object, you'll need to explicitly define the format.
datetime
Star
14297 Points
5797 Posts
Re: Misunderstanding this DateTime constructorSystem.Globalization.Calendar dteCal=System.Globali...
Feb 26, 2015 10:03 AM|gerrylowry|LINK
@mmacneill123
NEVER use TWO DIGIT years.
once upon a time there were PUNCHED CARDS*.
Foolishly, trying to make to most out of 80 columns, many application designers used ONLY the last two digits of the year.
The cost of this folly was millions and millons and millions of U.S. dollars. http://en.wikipedia.org/wiki/Year_2000_problem#Cost
USE four digit years.
Using two digit years saves you nothing in memory because ALL dates that use the DateTime struct are the same size. https://msdn.microsoft.com/en-us/library/system.datetime(v=vs.110).aspx
N.B.: if all of your dates are 20nn make your text box like this:
Year (21nn) [ ] 00-99 last two digits only.
The add 2000 server side.
* http://upload.wikimedia.org/wikipedia/commons/4/4c/Blue-punch-card-front-horiz.png:
datetime
Member
19 Points
89 Posts
Re: Misunderstanding this DateTime constructorSystem.Globalization.Calendar dteCal=System.Globali...
Feb 26, 2015 10:17 AM|mmacneill123|LINK
Thanks (diolch) Rion
Almost:
Appreciate the insights!
datetime
Member
19 Points
89 Posts
Re: Misunderstanding this DateTime constructorSystem.Globalization.Calendar dteCal=System.Globali...
Feb 26, 2015 10:20 AM|mmacneill123|LINK
Yes, totally agree. Unfortunately the Azure FTP directory listing returns the file date years as two digits. Yes ironic :-)
datetime
Star
14297 Points
5797 Posts
Re: Misunderstanding this DateTime constructorSystem.Globalization.Calendar dteCal=System.Globali...
Feb 26, 2015 10:26 AM|gerrylowry|LINK
@mmacneill123
would you mind explaining why you would not simply use this constructor with a 4-digit year?
https://msdn.microsoft.com/en-us/library/xcfzdy4x(v=vs.110).aspx
"DateTime Constructor (Int32, Int32, Int32)"
Parameters
The year (1 through 9999).
The month (1 through 12).
The day (1 through the number of days in month).
datetime
Star
9555 Points
2784 Posts
Re: Misunderstanding this DateTime constructorSystem.Globalization.Calendar dteCal=System.Globali...
Mar 04, 2015 11:03 PM|Paul Linton|LINK
datetime