【问题标题】:How to get epoch value from JQueryM datepicker?如何从 JQueryM datepicker 获取纪元值?
【发布时间】:2014-10-15 07:35:50
【问题描述】:

我正在尝试从我的日期选择器中获取纪元日期。我尝试通过 HTML 在日期选择器上放置一个带有 altFormat 的 altField,但是当尝试使用 JavaScript 取回该值时,它为空:

HTML -

 <input onchange='Refresh()' name="logging_from" id="logging_from" type="date" data-role="datebox" data-options='{"mode": "calbox","overrideDateFormat":"%d-%b-%y", "showInitialValue":true, "altField": "#epochdate","altFormat": "@" }'>

JavaScript -

    var loggingfromactual = document.getElementById("epochdate").value;

任何帮助将不胜感激,谢谢!

【问题讨论】:

    标签: javascript jquery jquery-mobile datepicker epoch


    【解决方案1】:

    这里有几个选项:

    处理输入的change事件,获取日期并转换为纪元(date.getTime()/1000)

    <input name="logging_from" id="logging_from" type="text" data-role="datebox" 
    data-options='{
       "useNewStyle":true, 
       "mode": "calbox",
       "overrideDateFormat":"%d-%b-%y"
    }' />
    
    $("#logging_from").on("change", function(){
        alert("Epoch of sel date is " + dateToEpoch($(this).datebox('getTheDate')));   
    });
    
    function dateToEpoch(thedate){
        return thedate.getTime() / 1000;   
    }
    

    DEMO

    ,在日历上添加关闭回调:

    <input name="logging_from" id="logging_from" type="text" data-role="datebox"
            data-options='{
               "useNewStyle":true, 
               "mode": "calbox",
               "overrideDateFormat":"%d-%b-%y" ,
                "closeCallback":"onCloseCalendar",
                "closeCallbackArgs":["out_date"]
            }' />
    
    function onCloseCalendar(thedate){
        alert("Epoch for sel date is " + dateToEpoch(thedate));
    }
    
    function dateToEpoch(thedate){
        return thedate.getTime() / 1000;   
    }
    

    DEMO

    【讨论】:

    • 谢谢,这很有帮助,但是有没有办法在不关闭日历的情况下获取日历的纪元值? - 因为我正在尝试存储 epoch 值,但它的 null 直到选择了日历上的两个日期(我在两个日历上使用它 - 到和从日期) - 谢谢。
    猜你喜欢
    • 2017-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-07
    • 2011-03-24
    • 2020-05-25
    • 2021-06-16
    • 1970-01-01
    相关资源
    最近更新 更多