text date conversion

G

Guest

Hi peeps

I have a datepicker control that's providing dates in the format dd/mm/yyyy
(UK).

I want to convert this to "yyyy-mm-dd" to store as a text field in my
database (had lots of problems with date conversions using "proper" data
fields).

I used to use ASP, and it was easy, using a combination of Left() Mid() and
Right(), but I can't work out how to do this conversion in C#.

Help appreciated!

Cheers


Dan
 
S

Shiva

DateTime.ToString("yyyy-MM-dd");

Hi peeps

I have a datepicker control that's providing dates in the format dd/mm/yyyy
(UK).

I want to convert this to "yyyy-mm-dd" to store as a text field in my
database (had lots of problems with date conversions using "proper" data
fields).

I used to use ASP, and it was easy, using a combination of Left() Mid() and
Right(), but I can't work out how to do this conversion in C#.

Help appreciated!

Cheers


Dan
 
G

Guest

Easy,

Split the string "mm/dd/yyyy" into an array using the delimiter "/" so that
you get:

[mm]
[dd]
[yyyy]

Then swap array positions so you get:

[yyyy]
[mm]
[dd]

Then join the array using the "-" character.

Why you would choose to store DATES as STRINGS is beyond me (just sounds
like a really, really newbie solution)...but if it floats your boat, then
there's your solution.
 
G

Guest

Hi Charles

Thanks for that. Yeah it was very newbie. The original system was written
with Access/ASP and as I said I had tremendous trouble with pulling dates out
the database.

Sussed that now, but im writing V2, it's just easier to continue with this
system than converting allllll the data :eek:)

Cheers for the answer!


Dan

Charles Chen said:
Easy,

Split the string "mm/dd/yyyy" into an array using the delimiter "/" so that
you get:

[mm]
[dd]
[yyyy]

Then swap array positions so you get:

[yyyy]
[mm]
[dd]

Then join the array using the "-" character.

Why you would choose to store DATES as STRINGS is beyond me (just sounds
like a really, really newbie solution)...but if it floats your boat, then
there's your solution.

dhnriverside said:
Hi peeps

I have a datepicker control that's providing dates in the format dd/mm/yyyy
(UK).

I want to convert this to "yyyy-mm-dd" to store as a text field in my
database (had lots of problems with date conversions using "proper" data
fields).

I used to use ASP, and it was easy, using a combination of Left() Mid() and
Right(), but I can't work out how to do this conversion in C#.

Help appreciated!

Cheers


Dan
 
W

William F. Robertson, Jr.

Dan,

You could always use the build in formatting on the DateTime object.

string s = "25/12/2004"
DateTime d = DateTime.ParseExact( s, "d/M/yyyy", null );

Now the variable 'd' will hold the date exactly as Dec 25, 2004.

You can get the date back out in any format you want using the .ToString()
method.

Console.WriteLine( d.ToString( "yyyy-MM-dd" ) )

Here are a couple links for DateTime formatting. These are gem pages for
working with any DateTime formatting issues.

http://msdn.microsoft.com/library/d...ide/html/cpconcustomdatetimeformatstrings.asp
http://msdn.microsoft.com/library/d...e/html/cpconstandarddatetimeformatstrings.asp

Happy formatting!

bill
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top