Formatting a "yyyyMMdd" string to "yyyy-MM-dd" in C#
-
Method name (only) changed. It shouldn't even have been public in the first place. Even if you disregard the blatant coding guidelines violations, I think this is a shame.
public string getFormattedDate(string date)
{
string concat ="";
if (null != date)
{
int i;
String dateTimeChar = new String(date.ToCharArray());
for(i=0;i<(date.Length-4);i++)
{
concat = concat+ dateTimeChar[i];
}
concat = concat+ ",";
for(i=4;i<(date.Length-2);i++)
{
concat = concat+ dateTimeChar[i];
}
concat = concat+ ",";
for(i=6;i<(date.Length);i++)
{
concat = concat+ dateTimeChar[i];
}DateTime dt = DateTime.Parse(concat); concat = dt.ToString("yyyy-MM-dd"); } return concat;
}
-
Method name (only) changed. It shouldn't even have been public in the first place. Even if you disregard the blatant coding guidelines violations, I think this is a shame.
public string getFormattedDate(string date)
{
string concat ="";
if (null != date)
{
int i;
String dateTimeChar = new String(date.ToCharArray());
for(i=0;i<(date.Length-4);i++)
{
concat = concat+ dateTimeChar[i];
}
concat = concat+ ",";
for(i=4;i<(date.Length-2);i++)
{
concat = concat+ dateTimeChar[i];
}
concat = concat+ ",";
for(i=6;i<(date.Length);i++)
{
concat = concat+ dateTimeChar[i];
}DateTime dt = DateTime.Parse(concat); concat = dt.ToString("yyyy-MM-dd"); } return concat;
}
Arrgggghhhhh - that's horrible. Let's ignore the fact that the method name is camel cased and hit a particular arrogant nasty. Unless you have a really, really, really good reason (and by that I mean that you can prove to me that you know me much much better than I know myself), don't presume to know how to format a date that I will like. I hate it when people force these cultural settings on me, and I have refused to use some software in the past because it has presented regional variations on me that I don't like because they are downright confusing.:mad: As an example, what date is this? 06/05/07 Is it the 6th May 2007, the 7th May 2006, the 5th of June 2007?
Deja View - the feeling that you've seen this post before.
-
Arrgggghhhhh - that's horrible. Let's ignore the fact that the method name is camel cased and hit a particular arrogant nasty. Unless you have a really, really, really good reason (and by that I mean that you can prove to me that you know me much much better than I know myself), don't presume to know how to format a date that I will like. I hate it when people force these cultural settings on me, and I have refused to use some software in the past because it has presented regional variations on me that I don't like because they are downright confusing.:mad: As an example, what date is this? 06/05/07 Is it the 6th May 2007, the 7th May 2006, the 5th of June 2007?
Deja View - the feeling that you've seen this post before.
It's a very good practice to ensure that any date strings you output to a file that will be consumed by another program have a fixed format. It doesn't much matter what that format is, but there's an ISO standard for yyyyMMdd, so that's what I normally use. (SQL Server is happy to consume that format and do so consistently whatever regional settings are applied).
Stability. What an interesting concept. -- Chris Maunder
-
Method name (only) changed. It shouldn't even have been public in the first place. Even if you disregard the blatant coding guidelines violations, I think this is a shame.
public string getFormattedDate(string date)
{
string concat ="";
if (null != date)
{
int i;
String dateTimeChar = new String(date.ToCharArray());
for(i=0;i<(date.Length-4);i++)
{
concat = concat+ dateTimeChar[i];
}
concat = concat+ ",";
for(i=4;i<(date.Length-2);i++)
{
concat = concat+ dateTimeChar[i];
}
concat = concat+ ",";
for(i=6;i<(date.Length);i++)
{
concat = concat+ dateTimeChar[i];
}DateTime dt = DateTime.Parse(concat); concat = dt.ToString("yyyy-MM-dd"); } return concat;
}
Ick. The obvious solution is
return date.Substring(0,4) + "-" + date.Substring(4,2) + "-" + date.Substring(6,2);
Goes nowhere near the risk of
DateTime.Parse
doing something different in a different locale.Stability. What an interesting concept. -- Chris Maunder
-
It's a very good practice to ensure that any date strings you output to a file that will be consumed by another program have a fixed format. It doesn't much matter what that format is, but there's an ISO standard for yyyyMMdd, so that's what I normally use. (SQL Server is happy to consume that format and do so consistently whatever regional settings are applied).
Stability. What an interesting concept. -- Chris Maunder
Yes, for outputting to a file or other method of "data interchange" use ISO 8601, for output to a person, allow that person's culture to format the date.
-
Method name (only) changed. It shouldn't even have been public in the first place. Even if you disregard the blatant coding guidelines violations, I think this is a shame.
public string getFormattedDate(string date)
{
string concat ="";
if (null != date)
{
int i;
String dateTimeChar = new String(date.ToCharArray());
for(i=0;i<(date.Length-4);i++)
{
concat = concat+ dateTimeChar[i];
}
concat = concat+ ",";
for(i=4;i<(date.Length-2);i++)
{
concat = concat+ dateTimeChar[i];
}
concat = concat+ ",";
for(i=6;i<(date.Length);i++)
{
concat = concat+ dateTimeChar[i];
}DateTime dt = DateTime.Parse(concat); concat = dt.ToString("yyyy-MM-dd"); } return concat;
}
Even ignoring the language, there are too many things wrong. The most important being that the characters are not being gathered based on their value.
while( date[i] == ‘y’ )
Concat += date[i++];Or something similar. I have know idea why the
new String
is even present, other than to waste time and memory. The given code is just asking for the call to pass the wrong format, so that it can throw an exception. In general, this code would receive a ‘D’ at best and an ‘F’ in any production code.INTP "Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra
-
It's a very good practice to ensure that any date strings you output to a file that will be consumed by another program have a fixed format. It doesn't much matter what that format is, but there's an ISO standard for yyyyMMdd, so that's what I normally use. (SQL Server is happy to consume that format and do so consistently whatever regional settings are applied).
Stability. What an interesting concept. -- Chris Maunder
I once got burned by a ASP/ access database site that would create utter crap when move from an english development to a german production server. (Or the other way round? I forget).
We are a big screwed up dysfunctional psychotic happy family - some more screwed up, others more happy, but everybody's psychotic joint venture definition of CP
My first real C# project | Linkify!|FoldWithUs! | sighist -
Method name (only) changed. It shouldn't even have been public in the first place. Even if you disregard the blatant coding guidelines violations, I think this is a shame.
public string getFormattedDate(string date)
{
string concat ="";
if (null != date)
{
int i;
String dateTimeChar = new String(date.ToCharArray());
for(i=0;i<(date.Length-4);i++)
{
concat = concat+ dateTimeChar[i];
}
concat = concat+ ",";
for(i=4;i<(date.Length-2);i++)
{
concat = concat+ dateTimeChar[i];
}
concat = concat+ ",";
for(i=6;i<(date.Length);i++)
{
concat = concat+ dateTimeChar[i];
}DateTime dt = DateTime.Parse(concat); concat = dt.ToString("yyyy-MM-dd"); } return concat;
}
-
Method name (only) changed. It shouldn't even have been public in the first place. Even if you disregard the blatant coding guidelines violations, I think this is a shame.
public string getFormattedDate(string date)
{
string concat ="";
if (null != date)
{
int i;
String dateTimeChar = new String(date.ToCharArray());
for(i=0;i<(date.Length-4);i++)
{
concat = concat+ dateTimeChar[i];
}
concat = concat+ ",";
for(i=4;i<(date.Length-2);i++)
{
concat = concat+ dateTimeChar[i];
}
concat = concat+ ",";
for(i=6;i<(date.Length);i++)
{
concat = concat+ dateTimeChar[i];
}DateTime dt = DateTime.Parse(concat); concat = dt.ToString("yyyy-MM-dd"); } return concat;
}
Superb HORROR
Regards, Sylvester G sylvester_g_m@yahoo.com