trimming date

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vinod allapu
    New Member
    • Apr 2009
    • 28

    trimming date

    Hi all

    I have a string like this "2/6/1986" which is a date..Remember that it is a date actually..so day size and month size are variant..It is not always single digit or single character...
    I want "1986","6", "2" individually...

    Can anybody provide trimming code to do this ..

    Thankful to you all,
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

    #2
    where do you need that? at the clientside you could use javascript like in the following example to achieve it:

    [CODE=javascript]// your value
    var val = '2/6/1986';

    // just split it at the slashes
    // to get an array with the separated
    // values
    var d = val.split('/');

    // in case you want it reverse orderd
    var d = val.split('/').reverse();
    [/CODE]
    or do you need a serverside solution? in this case you could have a look here

    kind regards

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      Well it's pretty easy server side too.
      You can refer to the properties of the Date Object....
      The following will return today's month, day, and year in individual variables:
      Code:
      Dim month As Integer = Now.Month
      Dim day As Integer = Now.Day
      Dim year As Integer = Now.Year

      Comment

      • vinod allapu
        New Member
        • Apr 2009
        • 28

        #4
        Hi boss,
        You did not get my actual problem.
        I am getting that date from the database and filling in data table.
        That data table is stored in session variable.
        In the redirected page in page_load i need to trim date,month and year and I need to use like this
        ddlDay.Value = trimmed day
        ddlMonth.Value = trimmed Month
        ddlYear.Value = trimmed Year

        Now help me in trimming the date string(code in C#) .Remember that it is a date actually..so day size and month size are variant..It is not always single digit or single character...
        Thanking you boss.

        Comment

        • gits
          Recognized Expert Moderator Expert
          • May 2007
          • 5390

          #5
          do you mean that you just want to know how to make a 2 from '02' ... just cast that string '02' to an integer ... that should be pretty easy?

          kind regards

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            I did miss read. You said that it's a String containing a date...not a Date Object.

            Well you could use the String.Split() method...splitt ing on the '/' character. The Split() method will return an array of Strings. Just like Gits recommended (only you'd use C# instead of JavaScript).

            Or you could use the DateTime.Parse( ) method to parse the string into a DateTime Object and then you will be able to grab the components like I had suggested earlier.

            Or you could use Regular Expressions to retrieve the components.


            There are many ways to solve this problem.

            Give one of them a try and let us know if you're having problems.

            -Frinny

            Comment

            Working...