Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8301921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T17:09:34+00:00 2026-06-08T17:09:34+00:00

I’m trying to build a javascript array of objects, parsing dates using date.js, and

  • 0

I’m trying to build a javascript array of objects, parsing dates using date.js, and then sort that aray of objects using date.js compare methods. Here is some code I put together to demonstrate my two problems.

Problem 1: How do I ignore and warn on dates that date.js can’t parse?
Problem 2: How can I sort an array of objects by those dates? (with or without date.js)

My intuition tells me that I’m doing this all wrong and there’s a better way. Ideas?

    <script type="text/javascript" src="depend/date.js"></script>

<script type="text/javascript">
var timedata = ["tomorrow", "today", "next thursday", "2012-08-02T04:00:00.000Z"]; // test data
var myArrayOfObjects = [];

//Iterate through the timedata array
for(var row = 0; row < timedata.length; row++){     
    var cleanrow = true;

    // Here is the first problem, on the 2012-08-02T04:00:00.000Z string can't be parsed (Date.parse(timestr) returns 0) 
    // even though it was previously created with .toISOString method below as a test
    // Error: Ignoring row 3, could not parse this date: 2012-08-02T04:00:00.000Z
    if (Date.parse(timedata[row])){ 
        time =  Date.parse(timedata[row]);
        isoTime = time.toISOString();
    }else{
        console.log("Ignoring row " + row + ", could not parse this date: " + timedata[row]);
        cleanrow = false;
    }
    var xdata = "dummytestdata";
    var weight = "dummytestdata";
    if(cleanrow) {
        myArrayOfObjects.push({x: xdata, xytime: isoTime, weight: weight});
    }
}

// Here is the second problem.  
// Error: TypeError: Date.parse(a.xytime) is null
myArrayOfObjects = myArrayOfObjects.sort(function(a,b){ return Date.parse(a.xytime).compareTo(Date.parse(b.xytime)); });

// Show that the objects are sorted by dates
for(var row = 0; row < myArrayOfObjects.length; row++) {
    console.log(myArrayOfObjects[row].xytime);
}
</script>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T17:09:35+00:00Added an answer on June 8, 2026 at 5:09 pm

    Couple of little things

    Consider declaring time and isoTime up in your var section, otherwise they just magically appear and the scoping is not defined.

    var cleanrow = true, isoTime = null, time = null;
    

    Consider parsing timedata[row] only once, and reusing the variable. It might clean up the code a bit.

    time = Date.parse(timedata[row])
    if (time){ 
        isoTime = time.toISOString();
    

    Is there any reason why you need the isoTime as a string, rather than just storing the date on the object you’re putting in the array? Strikes me that you could just store the date as well, and use that in the sort

    myArrayOfObjects.push({x: xdata, xytime: isoTime, weight: weight, realTime: time});
    //...
    myArrayOfObjects = myArrayOfObjects.sort(function(a,b){ return a.realTime.compareTo(b.realTime); });
    

    Also, looking at the mozilla doco, the sort method acts on the array in place, rather than returning a new array.

    myArrayOfObjects = myArrayOfObjects.sort(...
    //should be just
    myArrayOfObjects.sort(...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am reading a book about Javascript and jQuery and using one of the
I am trying to render a haml file in a javascript response like so:
We're building an app, our first using Rails 3, and we're having to build
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.