By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,711 Members | 2,144 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,711 IT Pros & Developers. It's quick & easy.

text date conversion

P: n/a
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
Nov 18 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
DateTime.ToString("yyyy-MM-dd");

"dhnriverside" <da*@musoswire.com> wrote in message
news:F9**********************************@microsof t.com...
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
Nov 18 '05 #2

P: n/a
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" wrote:
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

Nov 18 '05 #3

P: n/a
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 :o)

Cheers for the answer!
Dan

"Charles Chen" wrote:
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" wrote:
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

Nov 18 '05 #4

P: n/a
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/de...matstrings.asp
http://msdn.microsoft.com/library/de...matstrings.asp

Happy formatting!

bill

"dhnriverside" <da*@musoswire.com> wrote in message
news:F9**********************************@microsof t.com...
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

Nov 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.