NHD
NHD

Reputation: 445

Find closest date (javascript)

I want to find closest fromdt date from the xml.

 <?xml version="1.0" encoding="UTF-8"?>
    <schedule>
    <layout fromdt="2014-01-01 00:00:00" todt="2014-01-01 05:30:00"/>
    <layout fromdt="2014-02-01 00:00:00" todt="2014-01-01 05:30:00"/>
    <layout fromdt="2014-03-01 00:00:00" todt="2014-01-01 05:30:00"/>
    <layout fromdt="2014-04-05 00:00:00" todt="2014-01-01 05:30:00"/>
    <layout fromdt="2014-04-01 00:00:00" todt="2014-01-01 05:30:00"/>
    <layout fromdt="2014-05-01 00:00:00" todt="2014-01-01 05:30:00"/>
    <layout fromdt="2014-02-01 00:00:00" todt="2014-01-01 05:30:00"/>
    <layout fromdt="2014-02-21 00:00:00" todt="2014-01-01 05:30:00"/>
    <layout fromdt="2014-04-21 00:00:00" todt="2014-01-01 05:30:00"/>
    <layout fromdt="2014-04-10 00:00:00" todt="2014-01-01 05:30:00"/>
    </schedule>

I have tried using default date format "Wed Dec 04 2013 14:50:17 GMT-0800 (Pacific Daylight Time)" this work Properly..

var ScheduleDate=[];
    XmlResponse=function(xmlHttpRequest, status)
    {   
        ScheduleDate=[];
        $(xmlHttpRequest.responseXML)
        {
            var today=new Date();
            var xml=xmlHttpRequest.responseXML;
            $(xml).find("layout").each(function()
            {
            var fromDate=new Date($(this).attr('fromdt'));
            var toDate=new Date($(this).attr('to'));

            if(toDate>=today )
            {
                ScheduleDate.push({"from":fromDate,"to":toDate});
            }

            });
        }
        console.log(ScheduleDate);
        dateFunction();
    }
    function closestTime(days, testDate,property)
    {
        var bestDiff = null;
        var bestDate = 0;
        for(i = 0; i < days.length; ++i){
            currDiff = days[i][property] - testDate;
             if((currDiff < bestDiff || bestDiff == null)&& currDiff > 0){
                bestDate = days[i][property];
                bestDiff = currDiff;
            } else if (currDiff == bestDiff && days[i][property] > testDate) {
                //alert(i);
                bestDiff = currDiff;             
            }
        }

        return bestDate;
    }
function dateFunction()
    {
        var closestFrom=closestTime(ScheduleDate, new Date(),"from");

}

But When I try To use This format "YYYY-MM-DD HH:MM:SS" It return Invalid Date..

var fromDate=new Date("2014-01-01 00:00:00");

SO how can i take this input in date format or alternate Solution for this..

Upvotes: 2

Views: 1440

Answers (3)

Roberto
Roberto

Reputation: 9080

You don't need third-party libs for that kind of formatted date.

Use the built-in function Date.parse:

Some browsers (like Firefox), doesn't support given format, but you can convert it to ISO-8601)

var datestr = "2014-01-01 00:00:00";
var fromDate = new Date(Date.parse(datestr.replace(/ /, "T")));

In your code you can use this approach like:

var datestr = $(this).attr('fromdt');
var fromDate = new Date(Date.parse(datestr.replace(/ /, "T")));

Function doc at Mozilla MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse

Upvotes: 0

BAD_SEED
BAD_SEED

Reputation: 5056

You could use Date.parse, but this function returns a NaN in Firefox, so you have to make Date manually, with something like that:

function getDateByString(dateString) {
    var date = dateString.split(" ")[0]
    var time = dateString.split(" ")[1]

    var dd = date.split("-")[0]
    var mm = date.split("-")[1]
    var yyyy = date.split("-")[2]

    var hh = time.split(":")[0] 
    var mi = time.split(":")[1]
    var ss = time.split(":")[2]

    return new Date(dd, mm, yyyy, hh, mi, ss);
}

New Fiddle test.

Upvotes: 0

Colin Su
Colin Su

Reputation: 4609

2014-01-01 00:00:00 is not a valid format in standard.

I guess you're using Firefox, new Date("2014-01-01 00:00:00") only works in V8 JS engine (Chrome), you can use some alternative libraries such as Moment.js, then it will works on all platforms.

example.

var fromDate = moment("2014-01-01 00:00:00", "YYYY-MM-DD HH:mm:ss").toDate();

Upvotes: 1

Related Questions