View Full Version : Abridge String

01-12-2011, 09:29 PM
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...

01-12-2011, 09:55 PM
Is it always in exactly that format?

$monthday = substr($date,5,2).'/'.substr($date,8,2);

01-13-2011, 11:24 AM
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.

01-13-2011, 08:09 PM
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?

01-14-2011, 12:31 AM
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.

01-14-2011, 03:34 AM
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:

mydate = mydate.substring(5,10);
mydate = mydate.replace('-','/');

01-17-2011, 01:04 PM
Thanks a lot, I got it working well. Didn't realize the solution could be so simple...