在前台页面将时间戳改成年月日时分秒显示

这里以时间日期插件My97DatePicker来做示例:

<1>引包

<script type="text/javascript" src="lib/My97DatePicker/WdatePicker.js"></script>

<2>前台页面:

<div class="row cl">
    <label class="form-label col-2">有效时间:</label>
    <div class="formControls col-9">
        <input type="text" onfocus="WdatePicker({minDate:'#F{$dp.$D(\'endDate\')||\'%y-%M-%d\'}'})" id="startDate" name="startDate" class="input-text Wdate" style="width:120px;">-
        <input type="text" onfocus="WdatePicker({minDate:'#F{$dp.$D(\'startDate\')||\'%y-%M-%d\'}'})" id="endDate" name="endDate" class="input-text Wdate" style="width:120px;">
    </div>
</div>

<3>利用jQuery通过id取值:

$('#startDate').val(dates("Y-m-d",bizData.starttime));
$('#endDate').val(dates("Y-m-d",bizData.endtime));
<4>将下面的代码写入公用的common.js中,调用就可以了:(这里只用到了dates方法)

// 将时间戳改成年月日时分秒·
var timeFormat = function(ms, showDay) {

   if (typeof ms == 'string') {
      ms = parseInt(ms);
   }
   var timeLocal = new Date(ms);
   var year = timeLocal.getYear() + 1900;
   var month = timeLength(timeLocal.getMonth() + 1);
   var day = timeLength(timeLocal.getDate());
   var hour = timeLength(timeLocal.getHours());
   var minutes = timeLength(timeLocal.getMinutes());
   var second = timeLength(timeLocal.getSeconds());
   var weekDay = timeDay[timeLocal.getDay()];
   if (showDay == null) {
      var timeNew = year + "-" + month + "-" + day;
   } else if (!showDay) {
      var timeNew = year + "-" + month + "-" + day + " " + hour + ":"
            + minutes + ":" + second;
   } else {
      var timeNew = year + "-" + month + "-" + day + " " + hour + ":"
         + minutes + ":" + second + " " + weekDay;
   }
   return timeNew;
};

// 将时间戳改成年月日时分秒·
var dates = function(format,ms) {
   if ( typeof ms == 'string') {
      ms = parseInt(ms);
   }
   var timeLocal = new Date(ms);
   var year = timeLocal.getFullYear();
   var month = timeLength(timeLocal.getMonth() + 1);
   var day = timeLength(timeLocal.getDate());
   var hour = timeLength(timeLocal.getHours());
   var minutes = timeLength(timeLocal.getMinutes());
   var second = timeLength(timeLocal.getSeconds());
   return format.replace("Y",year).replace("m",month).replace("d",day).replace("H",hour).replace("i",minutes).replace("s",second);
};


你可能感兴趣的:(插件,my97datepicker,时间戳改成年月日)