text date conversion

Discussion in 'ASP .Net' started by =?Utf-8?B?ZGhucml2ZXJzaWRl?=, Dec 1, 2004.

  1. 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
    =?Utf-8?B?ZGhucml2ZXJzaWRl?=, Dec 1, 2004
    #1
    1. Advertising

  2. =?Utf-8?B?ZGhucml2ZXJzaWRl?=

    Shiva Guest

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

    "dhnriverside" <> wrote in message
    news:...
    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
    Shiva, Dec 1, 2004
    #2
    1. Advertising

  3. 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
    =?Utf-8?B?Q2hhcmxlcyBDaGVu?=, Dec 1, 2004
    #3
  4. 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" 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
    =?Utf-8?B?ZGhucml2ZXJzaWRl?=, Dec 1, 2004
    #4
  5. 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

    "dhnriverside" <> wrote in message
    news:...
    > 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
    William F. Robertson, Jr., Dec 1, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matt
    Replies:
    1
    Views:
    589
    Matthew Speed
    Nov 8, 2003
  2. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,230
    Michael Borgwardt
    May 30, 2004
  3. Matt
    Replies:
    2
    Views:
    505
    Pete Becker
    Nov 8, 2003
  4. Matt
    Replies:
    3
    Views:
    709
    Richard Heathfield
    Nov 8, 2003
  5. Sanjeeb
    Replies:
    2
    Views:
    173
Loading...

Share This Page