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 6551551
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:21:06+00:00 2026-05-25T12:21:06+00:00

Im trying to get the closest date to today from array dates. Example: Today

  • 0

Im trying to get the closest date to today from array “dates”.

Example: Today is 2011-09-10 -> next closest date from JSON file is “2012-12-20” -> $(‘div’).append(‘date1: ‘ + dates.date1);

Example 2: Today is 2012-12-21 -> next closest date from JSON file is “2012-12-25” -> $(‘div’).append(‘date2: ‘ + dates.date2);

Tried a few solutons, but without success. Need help! Here is JSON file.

[
    {
        "fields": {
            "field1": "Field1",
            "field2": "Field2"
        },
        "dates": {
            "date1": "2012-12-20",
            "date2": "2012-12-25",
            "date3": "2012-12-31"
        }
    }
]
  • 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-05-25T12:21:07+00:00Added an answer on May 25, 2026 at 12:21 pm

    Whoever it was that had a solution like this…. put it back please 🙂

    var json = [
        {
        "fields": {
            "field1": "Field1",
            "field2": "Field2"
        },
        "dates": {
            "date1": "2012-12-20",
            "date2": "2012-12-25",
            "date3": "2009-12-31",
            "date4": "2010-12-31"
        }}
    ]
    var dates = [];
    var jsondates = json[0].dates;
    for (var i in jsondates) {
        dates.push(new Date(jsondates[i]))
    }
    dates.sort(function(a,b){
      return Math.abs(1 - a / new Date()) - Math.abs(1 - b / new Date())
    });
    
    // dates is now ordered by closest to today with index 0 being the closest.
    

    all it needed was a sort algorithm

    Edit

    Just read the question more closely… you don’t want any dates previous too current date to be considered even if it is closer… this is not to hard either:

    for (var i in jsondates) {
        if(new Date(jsondates[i]) >= new Date())
            dates.push(new Date(jsondates[i]))
    }
    dates.sort(function(a,b){
      return Math.abs(1 - a / new Date()) - Math.abs(1 - b / new Date())
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to get this example working from http://www.munna.shatkotha.com/blog/post/2008/10/26/Light-box-effect-with-WPF.aspx However, I can't seem to get
Trying to get a JSON output to work with jqGrid 'userdata' option. The example
Im trying to get a form element value using closest. Here some example code.
I have the following jQuery: $(this).closest('div.mcoup').find('div.delcoup').slideToggle(400) .siblings().children('div.delcoup').slideUp(400); What I'm trying to do is get
I'm trying get values from a GridView using the following code: foreach (GridViewRow row
Trying to get parameters from a PUT request using HttpServlet#doPut: public void doPut(HttpServletRequest request,
I am trying to get a document from Excel or OpenOffice that contains UTF
Not sure how to describe what I'm trying to get from this question, but
When trying to get some data on my android client from my webservice (running
I am trying to get the index of the closest character to a point.

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.