Results 1 to 7 of 7

Thread: Abridge String

  1. #1
    Join Date
    Nov 2010
    Posts
    28
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Question Abridge String

    Simple problem, perhaps with a simple solution. I am calling a date string from an XML document which gives me the following output (for example):

    2011-01-12 08:35

    I want to display just the month and the day (the 01-12) section. Basically, how can I extract just those numbers and then convert the hyphen (-) to a slash (/)?

    thanks in advance...

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Is it always in exactly that format?
    PHP Code:
    $monthday substr($date,5,2).'/'.substr($date,8,2); 
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Nov 2010
    Posts
    28
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Possible Code:

    Yes, it is always in exactly that format (with different numbers, naturally). Here's some code I saw that I thought about using, but doesn't work for me yet:

    Prior to this code, I call the XML date tag content and store it as a string to var itemDate:

    itemDate = itemDate.replace(" ","-");
    itemDate = itemDate.replace(":","-");
    var dArray = itemDate.split("-");
    var d = new Date(dArray[0],dArray[1]-1,dArray[2]);
    var m_names = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");

    var curr_date = d.getDate();
    var curr_month = d.getMonth();
    var curr_year = d.getFullYear();
    itemDate = curr_date + " " + m_names[curr_month];

    This code should work, shouldn't it? I'd really like use JavaScript to solve this problem. By the way, it it makes much difference, I'm using this code in a Google Gadget...

    Thanks again.

  4. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Oh, I'm sorry. I thought this was a question about PHP.

    You are extracting this as a string from XML, correct? The code you posted above is old code that you do not plan to use any more?
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  5. #5
    Join Date
    Nov 2010
    Posts
    28
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default

    No, I may use that code, but I can't get it to work. Does it work for you? I got that code from another google gadget doing a similar thing.

  6. #6
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    What you posted doesn't match what you are describing. You are using names for the months, not numbers. You should not need to use a date object unless you are accessing the current date.
    If you just want to get the numbers as described in your first post, you should be able to do it using only these two functions:
    http://www.w3schools.com/jsref/jsref_substring.asp
    http://www.w3schools.com/jsref/jsref_replace.asp

    Code:
    mydate = mydate.substring(5,10);
    mydate = mydate.replace('-','/');
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  7. The Following User Says Thank You to djr33 For This Useful Post:

    Fighterfox (01-17-2011)

  8. #7
    Join Date
    Nov 2010
    Posts
    28
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Wink

    Thanks a lot, I got it working well. Didn't realize the solution could be so simple...

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
  •