A new way to create a DateTime from the year!
-
Looking for a error in a project, I saw this code.
IFormatProvider culture = new System.Globalization.CultureInfo("en-US", true);
string year = itemDT["YEARBARRIER"].ToString();string dateYear = "01" + "/" + "01" + "/" + year;
DateTime convertedDate = DateTime.Parse(dateYear, culture, System.Globalization.DateTimeStyles.AssumeLocal);
objBarrierFacade.YearBarrier = convertedDate;
That can be writed with only one line.
objBarrierFacade.YearBarrier = new DateTime((int)itemDT["YEARBARRIER"], 1, 1);
But I think that my solution is so easy and so obviuos to be used and other programmers need a good reason to spend more than a week in some easy use cases.
-
Looking for a error in a project, I saw this code.
IFormatProvider culture = new System.Globalization.CultureInfo("en-US", true);
string year = itemDT["YEARBARRIER"].ToString();string dateYear = "01" + "/" + "01" + "/" + year;
DateTime convertedDate = DateTime.Parse(dateYear, culture, System.Globalization.DateTimeStyles.AssumeLocal);
objBarrierFacade.YearBarrier = convertedDate;
That can be writed with only one line.
objBarrierFacade.YearBarrier = new DateTime((int)itemDT["YEARBARRIER"], 1, 1);
But I think that my solution is so easy and so obviuos to be used and other programmers need a good reason to spend more than a week in some easy use cases.
my rule of thumb is: when some problem needs a lot of coding, there should be a shorter way to do it. Look for it! Software library functionalities like .net's are based on the survey of the necessities of millions of developers. This rule, as every one, has some exceptions.
Best regards, Jaime.
-
Looking for a error in a project, I saw this code.
IFormatProvider culture = new System.Globalization.CultureInfo("en-US", true);
string year = itemDT["YEARBARRIER"].ToString();string dateYear = "01" + "/" + "01" + "/" + year;
DateTime convertedDate = DateTime.Parse(dateYear, culture, System.Globalization.DateTimeStyles.AssumeLocal);
objBarrierFacade.YearBarrier = convertedDate;
That can be writed with only one line.
objBarrierFacade.YearBarrier = new DateTime((int)itemDT["YEARBARRIER"], 1, 1);
But I think that my solution is so easy and so obviuos to be used and other programmers need a good reason to spend more than a week in some easy use cases.
Yes, kind of, give or take some input data checks.
cheers, Chris Maunder The Code Project Co-founder Microsoft C++ MVP
-
Looking for a error in a project, I saw this code.
IFormatProvider culture = new System.Globalization.CultureInfo("en-US", true);
string year = itemDT["YEARBARRIER"].ToString();string dateYear = "01" + "/" + "01" + "/" + year;
DateTime convertedDate = DateTime.Parse(dateYear, culture, System.Globalization.DateTimeStyles.AssumeLocal);
objBarrierFacade.YearBarrier = convertedDate;
That can be writed with only one line.
objBarrierFacade.YearBarrier = new DateTime((int)itemDT["YEARBARRIER"], 1, 1);
But I think that my solution is so easy and so obviuos to be used and other programmers need a good reason to spend more than a week in some easy use cases.
The only comment I would make is that you have dropped the CultureInfo from your solution. Not sure how important that is for your project, but it could be significant. For example, under the Islamic calendar the current year is 1430. That would give a very different DateTime value than the Gregorian calendar used in the US.
-
The only comment I would make is that you have dropped the CultureInfo from your solution. Not sure how important that is for your project, but it could be significant. For example, under the Islamic calendar the current year is 1430. That would give a very different DateTime value than the Gregorian calendar used in the US.
I now that. But as you can see, the developer forced a CultureInfo to ensure a date format valid to parse his string. In the case explaned this is not necessary because the developer only need to create a datetime object instace from a int value representing a year.
-
my rule of thumb is: when some problem needs a lot of coding, there should be a shorter way to do it. Look for it! Software library functionalities like .net's are based on the survey of the necessities of millions of developers. This rule, as every one, has some exceptions.
Best regards, Jaime.
Yeah, it's the objective of this post. Some programmer use a lot of code because they don't know a easier way to do that.
-
I now that. But as you can see, the developer forced a CultureInfo to ensure a date format valid to parse his string. In the case explaned this is not necessary because the developer only need to create a datetime object instace from a int value representing a year.
...unless you run the code with a different default Culture, in which case you may get a different DateTime value for the same year input depending on the default calendar in use. The original code will always produce the same result since it specifies which CultureInfo to use. I'm not saying the original code is right, I am merely pointing out that strictly speaking your replacement code is not functionally equivalent to the original. Probably not an issue if your code will never run outside the US but, hey, I'm a geek and pedantry is part of the lifestyle.
-
Yes, kind of, give or take some input data checks.
cheers, Chris Maunder The Code Project Co-founder Microsoft C++ MVP
Huh? How do you know there is input data involved here or that it's not known to be in a valid state?