1

I'm trying to create an array that looks like this:

([
    [Date.UTC(2003,8,24),8709],
    [Date.UTC(2006,8,25),872],
    [Date.UTC(2002,8,26),8714],
    [Date.UTC(2009,8,29),8638],
    [Date.UTC(2000,8,30),8567]
]);

I can create a random date, but I can't figure out how to add it to an array in that particular format. Here's some code, and a Fiddle.

function randomDate(start, end) {
    return new Date(start.getTime() + Math.random() * (end.getTime() - start.getTime()))
}

$(function() {
    for(var i = 0; i < 1000; i++) {
        var date = randomDate(new Date(2004, 0, 9), new Date());

        // instead of printing to a div, it'd save to an array
        $('#dates').append(date); 
    }
});
7
  • 2
    Update your fiddle with ur code Commented Feb 15, 2013 at 14:53
  • Print to file where exactly? Are you trying to send it to your server, or create files on the users computer? Commented Feb 15, 2013 at 14:53
  • Just to a txt file. The chart is currently created from a JSON request to a Highcharts sample data file. Commented Feb 15, 2013 at 14:55
  • To a text file where? The only thing you have access to is your own server, and that would require submitting something or sending an ajax request ? Commented Feb 15, 2013 at 14:56
  • 1
    Dont use alert in a loop of 1000 please use console.log Commented Feb 15, 2013 at 14:58

2 Answers 2

3
$(function() {
    var arr = [];
    for(var i = 0; i < 1000; i++) {
        var date = randomDate(new Date(2004, 0, 9), new Date());
        var randNum = Math.round(Math.random()*10000);
        arr.push([date, randNum]);
    }
});
Sign up to request clarification or add additional context in comments.

1 Comment

Looks good, but how can I get it in the Date.UTC format above? Do I just have to pull out the day/month/year manually?
-1

JavaScript can not save to a local file, your best bet is to force the output to another window and let the user save it locally.

window.open("data:text/json;charset=utf-8," + "Your Date List"));

2 Comments

The question is not about saving the data but creating the data in the first place.
Do you want the printed date format in the array or can it just be a long?

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.