如何从JQuery datepicker获取epoch值?

时间:2014-08-21 15:27:34

标签: javascript jquery jquery-mobile datepicker epoch

我正试图从我的datepicker获取纪元日期。我尝试通过HTML在datepicker上放置一个带altFormat的altField,但是当尝试使用JavaScript获取值时,它为null:

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;

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

1 个答案:

答案 0 :(得分:0)

以下是几个选项:

处理输入的更改事件,获取日期并转换为纪元(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