Formatting a String to a Date String

  • Thread starter Thread starter Bishman
  • Start date Start date
B

Bishman

This is driving me mad, should be simple,

How can I format a date string "01122006" to be "01/12/2006"

I have tried
string teststr = String.Format("{d}","01122006");

AND

string teststr = String.Format("{##/##/####","01122006");

and various other methods but these throw errors.

Can someone help please ?

Thanks

Jon.
 
You need to make it a datetime first, Try:

DateTime.ParseExact("01122006", "ddMMyyyy",
DateTimeFormatInfo.CurrentInfo).ToString("dd/MM/yyyy");

Ciaran O'Donnell
 
if you set sDate = "01122006"
You may try
String.Format("{0}/{1}/{2}",sDate.substring(0,2),sDate.substring(2,2),sDate.substring(4,4))

Gustavo.
 
Thanks for the replies,

I must have got mixed up with other types of formatting !

I will give these a go,

Jon.
 
ParseExact is the way to go. However if you want to manipulate the day month
and year separately (catering for different cultures) you could to it this
way:

//
// Create a regex
//
Regex r = new Regex(@"(?<day>\d{2})(?<month>\d{2})(?<year>\d{4})");
//
// Extract the values
//
Match m = r.Match("01022006");
int day = int.Parse(m.Groups["day"].Value);
int month = int.Parse(m.Groups["month"].Value);
int year = int.Parse(m.Groups["year"].Value);
//
// Now that we have the components separately,
// Construct the datetime
//
DateTime date= new DateTime(year,month,day);
 
In my idea the only good answer until now,

All other answers are converting from string to a DateTime structure.

:-)

Cor
 
Cor Ligthert said:
In my idea the only good answer until now,

All other answers are converting from string to a DateTime structure.

:-)

It's entirely reasonable to convert to a DateTime structure *and then
format that*, just as in Ciaran's response.

The problem with *not* going via a DateTime is that you have no error
checking. For instance, Gustavo's code would convert "abcdef" into
"ab/cd/ef" instead of (correctly, IMO) noticing that the string isn't a
valid date.
 
In my idea the only good answer until now,
All other answers are converting from string to a DateTime structure.

:-)

Cor
Sorry, but going thru a DateTime is the best thing.
Then you can format it the way you want. And ##/##/## is not how you want,
for a properly internationalized application.
 
It's entirely reasonable to convert to a DateTime structure *and then
format that*, just as in Ciaran's response.

The problem with *not* going via a DateTime is that you have no error
checking. For instance, Gustavo's code would convert "abcdef" into
"ab/cd/ef" instead of (correctly, IMO) noticing that the string isn't a
valid date.

The advance with *not* going to a DateTime structure is that you have not
any problem with inbuild verification as in the DateTime structure as by
instance check on leapmonths, way of writting or whatever. There are
situations where people ignore that expressely with good reasons.

If you want the advantages of a DateTime structure, than you should use a
DateTime structure from the beginning not only to set a position extra.

Cor

Cor
 
Cor Ligthert said:
The advance with *not* going to a DateTime structure is that you have not
any problem with inbuild verification as in the DateTime structure as by
instance check on leapmonths, way of writting or whatever. There are
situations where people ignore that expressely with good reasons.

I can't remember ever being in the situation where I *want*, say,
Februrary 29th 2005 to be a valid input. Can you give an example where
that's the case?
If you want the advantages of a DateTime structure, than you should use a
DateTime structure from the beginning not only to set a position extra.

What do you mean by "not only to set a position extra"?
 
When importing data from an old DOS programme, I once had invalid
dates like February 29th 2005. I reasoned that since people were
happy with them in the original programme, they would be happy
with them in the new one too. Turning them into a valid date would
have been very tricky, trying to second guess what the user intended.
 
Jay said:
When importing data from an old DOS programme, I once had invalid
dates like February 29th 2005. I reasoned that since people were
happy with them in the original programme, they would be happy
with them in the new one too. Turning them into a valid date would
have been very tricky, trying to second guess what the user intended.

Fair enough. I would hope that situations like that are a rarity rather
than a common requirement though :)

Jon
 
Cor Ligthert said:
29 february is not in a leap month it is a date in a leapyear in the
Gregorian calendar, a leapmonth is something else in the Hebrewic calendar,
I was sure that you would give an answer like you did.

http://en.wikipedia.org/wiki/Hebrew_calendar

The rest is answered by Jay.

So in a few very specific cases, you'd want different validation, or
perhaps no validation at all. That's fine, but I don't think that's
*nearly* as common a requirement as "normal" date checking, which is
where using DateTime is the way to go.
 
You think I know.

Cor

Jon Skeet said:
So in a few very specific cases, you'd want different validation, or
perhaps no validation at all. That's fine, but I don't think that's
*nearly* as common a requirement as "normal" date checking, which is
where using DateTime is the way to go.
 
Maybe you don't understand my answer.

If a document is written with a date, you cannot change that date, than it
is worthless for the law.

The sample from the OP is typical for cases were original dates comes from
documents or have there origin from documents. (It is of course possible
that it is not in this case, it can as well come from a webpage, however
than it still exist as it is by instance an accepted order, in that case the
error should be handled before the accept).

If you follow sport than you will would know that probably Landis will be
spoken free of doping just because of a wrong number in the accusing
documents.

Cor
 
Cor Ligthert said:
Maybe you don't understand my answer.

If a document is written with a date, you cannot change that date, than it
is worthless for the law.

The sample from the OP is typical for cases were original dates comes from
documents or have there origin from documents. (It is of course possible
that it is not in this case, it can as well come from a webpage, however
than it still exist as it is by instance an accepted order, in that case the
error should be handled before the accept).

*If* that is the case, then it's fair enough - although I'd usually at
least generate a warning to highlight the incorrect data.
If you follow sport than you will would know that probably Landis will be
spoken free of doping just because of a wrong number in the accusing
documents.

That sounds like an argument *for* validation rather than against it.
The earlier the data is noticed to be incorrect, the better in my
experience.
 

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

Back
Top