1,105,328 Community Members

Date problem.

Member Avatar
Begginnerdev
Veteran Poster
1,141 posts since Apr 2010
Reputation Points: 251 [?]
Q&As Helped to Solve: 191 [?]
Skill Endorsements: 15 [?]
 
0
 

Hello my fellow daniwebbers, I have yet another question.

I have a date, which is pulled from a database, that I need to convert to another form of that date.

Example.

Date1: 1/2/2001
Date2: 01/02/2001

What i am wanting to do is turn the current date format into two digit month/day with four digit year.

Here is the problem.

Upon converting the date, the return result is the format string??

What I have tried:

String.Format
String.FormatDateTime
DateTime.TryPaseExact

Anyone have any ideas?

Member Avatar
Mitja Bonca
Posting Maven
2,561 posts since May 2009
Reputation Points: 557 [?]
Q&As Helped to Solve: 489 [?]
Skill Endorsements: 21 [?]
 
0
 

try it this way:

 string date1 = "1/2/2001";
 DateTime date = Convert.ToDateTime(date1);
 //this is what you have to do:
 string date2 = date.ToString("MM/dd/yyyy", System.Globalization.CultureInfo.InvariantCulture);

My 1st two lines of code are only example to get an actual date (you already have a date from database).

Member Avatar
Begginnerdev
Veteran Poster
1,141 posts since Apr 2010
Reputation Points: 251 [?]
Q&As Helped to Solve: 191 [?]
Skill Endorsements: 15 [?]
 
0
 

Found the solution.

Had to double parse the dates.

One string split to remove the space, then another to remove the /'s

Then use the pad function to add the 0's.

On the other hand, thanks for the suggestions Bonca.

Question Answered as of 2 Years Ago by Mitja Bonca
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: