SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Member
    Join Date
    Mar 2011
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    converting textbox.text

    I have a form on my web site that is going to be dates that the user can enter. What I'd like to do is to convert it after they enter it and store that as a variable.

    Currently the way that they are able to enter the date is mm/dd/yyyy and I'd like them to enter it yyyy/mm/dd. How can I reformat that data?

    Thank you

    Doug

  2. #2
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hi Doug. Welcome to Sitepoint.

    Essentially you need to split the date given into an array of 3 items, where the / is the splitting character. Then reorganise into the right order and then join the array back to a string. That's one way of doing it.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  3. #3
    Resident OCD goofball! bronze trophy Serenarules's Avatar
    Join Date
    Dec 2002
    Posts
    1,911
    Mentioned
    26 Post(s)
    Tagged
    0 Thread(s)
    string dts = DateTime.Parse("03/22/2011").ToString("yyyy/mm/dd");

  4. #4
    SitePoint Member
    Join Date
    Mar 2011
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Jake and Serena,

    Thank you both for the information.

    Serena in your code I'm assuming dts is a variable that I'd declared earlier as a string?

  5. #5
    Resident OCD goofball! bronze trophy Serenarules's Avatar
    Join Date
    Dec 2002
    Posts
    1,911
    Mentioned
    26 Post(s)
    Tagged
    0 Thread(s)
    Basically. It depends on what system type you actually want. I converted from string back to string, but you could leave it as a DateTime is your backend stores it like that.

    Parse takes any proper date format and creates a DateTime object from it. We then take that result and use ToString, which takes a format for output. Nice and simple.

    So if you want to set a label on postback.

    if (IsPostBack)
    {
    myLabel.Text = DateTime.Parse(dateTimeInput.Text).ToString("yyyy/mm/dd");
    }

    If they input any recognised date, it'll spit it back out the way you want it. They could input "January 3, 2009" and get back "2009/01/03".

  6. #6
    SitePoint Member
    Join Date
    Mar 2011
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Serena,

    would this go in my asp.net page or in the vb.net that I'm using? I'm using .net for essentially the visual aspect of the page, but using vb.net for all the mechanical aspects of the page (like executing my queries, the load events, etc etc.)

  7. #7
    Resident OCD goofball! bronze trophy Serenarules's Avatar
    Join Date
    Dec 2002
    Posts
    1,911
    Mentioned
    26 Post(s)
    Tagged
    0 Thread(s)
    That's up to you and how you've assembled things. I'm not privy to that so I really can't answer that question. Just know that you can put the basic conversion code anywhere you want and use the result as you wish. Personally, I'd create a utility class with this method in it.

    public string ConvertDate(string existing)
    {
    return DateTime.Parse(existing).ToString("yyyy/mm/dd");
    }

    You could even grab the format from an app setting instead of a hard coded value. In addition, you might want to use TryParse or wrap the code in a try-catch block of your own, as it will generate an error if the input is not a recognised date.

  8. #8
    SitePoint Mentor NightStalker-DNS's Avatar
    Join Date
    Jul 2004
    Location
    Cape Town, South Africa
    Posts
    2,880
    Mentioned
    48 Post(s)
    Tagged
    0 Thread(s)
    That code goes in the code behind page. Force the user to input in the correct format then use DateTime.parse. Use javascript to force format.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •