1

New to Javascript and jQuery, so I assume this is a pretty simple question.

I have a date I want to split into a string, then print out the array.

var date = "12/10/2010";
var dateArray = date.split(/);

$('#printbox').html($(dateArray[0]));

<span id="printbox"></span>


Edit: The question is, how can I split "12/10/2010" into an array like this:

Array[0] = 12
Array[1] = 10
Array[3] = 2010

And how do I print them to HTML?

1
  • What's your question...what's the problem? Commented Oct 5, 2010 at 14:51

6 Answers 6

10

The first parameter to the string.split function should be a string or regex:

var date = "12/10/2010";
var dateArray = date.split("/");

// dateArray[0] == 12
// dateArray[1] == 10
// dateArray[2] == 2010

// Now use the array to print out whichever way you want (jQuery.html(values))...
Sign up to request clarification or add additional context in comments.

8 Comments

This seems nice, the Array is just not outputting to HTML.
Out of curiosity, what does putting the first element of the array into a jQuery construct do? Does it print out all values of the array? I haven't seen this before.
I realize this is a carryover from the question, but it doesn't make sense to wrap dataArray[0] in a jQuery object. :o) Better would be to do a .join(), like .html( dateArray.join(', ') ).
@stjowa - if you look at the example of the OP, the HTML markup has a div with id printbox, the jQuery simply sets the contents of that div with the passed in string. See .html
@Oded - I was referring to $(dateArray[0]). This would have translated to $(12) and didn't make sense as patrick dw described. But, I see it got removed.
|
2
var date = "12/10/2010";
var dateArray = date.split('/');

$('#printbox').html(dateArray[0]); // it displays 12 in printbox
$('#printbox').html(dateArray[1]); // it displays 10 in printbox
$('#printbox').html(dateArray[2]); // it displays 2010 in printbox

2 Comments

Each subsequent call to .html() will overwrite the previous.
Of course ! It was to help him to make a choice. It was obvious for me but you're right, not for everybody...
1

For splitting you were almost there :)

var dateArray = date.split("/");

If you want to use jQuery you could use the each method.

$.each(dateArray, function(index, value) { 
  // print out here
});

This constructs a loop that iterates over the elements in your array. You can access this element by the value variable.

1 Comment

It works perfect. I just used it in my jquery mobile project. I have spitted values <code> var fromService = data.split(";"); $.each(fromService, function (index, value) { // print out here $('#townlist li:last-child').removeClass(); $("#townlist").append("<li class='ui-last-child'><a href='#' class='ui-btn ui-btn-icon-right ui-icon-carat-r'>ITEM</a></li>"); }); $("#townlist").listview("refresh"); </code>
1
var date = "12/10/2010";
var dateArray = date.split('/');

// dateArray => ['12', '10', '2010']

If you want to use jQuery there is an each function that you can use:

$.each(dateArray, function (index, value) {
    $('#printbox').append(value);
});

There is also a .forEach method in newer browsers:

dateArray.forEach(function (value) {
    document.getElementById('printbox').
        appendChild(document.createTextNode(value));
});

Older browsers can have this method via the Array.prototype:

if (!(Array.prototype.forEach === 'function')) {
    Array.prototype.forEach = function (fn) {
        for (var i = 0, l = this.length; i < l; i += 1) {
            fn(this[i]);
        }
    };
}

Comments

0
var date = "12/10/2010";
var dateArray = date.split(/\//); // have to quote regular expressions with /

document.write; // this does nothing

$('#printbox').html(dateArray[2] + '-' + dateArray[0] + '-' + dateArray[1]); // do what you want here. The code I provided should print 2010-12-10

1 Comment

I don't see the point of regex in .split(), doing .split('/') is simpler and does the exact same thing.
0
var date = "12/10/2010";
var dateArray = date.split('/');

var year = dateArray.splice(2,1);
dateArray[3] = year;

should give you

Array[0] = 12
Array[1] = 10
Array[3] = 2010

But why you need the year in the fourth key I don't know. (also removes from the third so you don't end up with two years)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.