简体   繁体   English

查找最接近的日期(javascript)

[英]Find closest date (javascript)

I want to find closest fromdt date from the xml. 我想从xml中找到最接近的fromdt日期。

 <?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.. 我已经尝试使用默认的日期格式“ 2013年12月4日星期三14:50:17 GMT-0800(太平洋夏令时)”正常工作。

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.. 但是,当我尝试使用此格式“ YYYY-MM-DD HH:MM:SS ”时,它返回无效日期。

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.. 所以我该如何以日期格式或替代解决方案来接受此输入。

2014-01-01 00:00:00 is not a valid format in standard. 2014年1月1日00:00:00不是标准有效格式。

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. 我想您使用的是Firefox, new Date("2014-01-01 00:00:00")仅适用于V8 JS引擎(Chrome),您可以使用某些替代库,例如Moment.js ,那么它将起作用在所有平台上。

example. 例。

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

You don't need third-party libs for that kind of formatted date. 这种格式的日期不需要第三方库。

Use the built-in function Date.parse : 使用内置函数Date.parse

Some browsers (like Firefox), doesn't support given format, but you can convert it to ISO-8601) 某些浏览器(例如Firefox)不支持给定的格式,但是您可以将其转换为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 Mozilla MDN上的功能文档: https : //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse

You could use Date.parse , but this function returns a NaN in Firefox, so you have to make Date manually, with something like that: 您可以使用Date.parse ,但是此函数在Firefox中返回NaN ,因此您必须手动创建Date ,如下所示:

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. 新的Fiddle测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM