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
DanDateTime.ToString("yyyy-MM-dd");
"dhnriverside" <dan@.musoswire.com> wrote in message
news:F96893C8-D39B-4207-B3C9-861A42594CD5@.microsoft.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
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
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
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...rmatstrings.asp
http://msdn.microsoft.com/library/d...rmatstrings.asp
Happy formatting!
bill
"dhnriverside" <dan@.musoswire.com> wrote in message
news:F96893C8-D39B-4207-B3C9-861A42594CD5@.microsoft.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
0 comments:
Post a Comment