搜档网
当前位置:搜档网 › js时间的format函数

js时间的format函数

js时间的format函数
js时间的format函数

Js 时间的format

function setSearchTimeArea() {

debugger;

//取窗体定义数据

var searchData = commonUtils.getInstanceComponent("searchData");

var dateValue = searchData.getValueByName("dateValue");

//定义查询开始时间和结束时间

var time1 = new Date();

var time2 = new Date();

//设置查询时间区间

switch (dateValue) {

case "01" :

time1.setMonth(time2.getMonth() - 1);

time1.setHours(0);

time1.setMinutes(0);

time1.setSeconds(0);

break;

case "02" :

time1.setMonth(time2.getMonth() - 3);

time1.setHours(0);

time1.setMinutes(0);

time1.setSeconds(0);

break;

case "03" :

time1.setMonth(time2.getMonth() - 6);

time1.setHours(0);

time1.setMinutes(0);

time1.setSeconds(0);

break;

case "04" :

time1.setMonth(time2.getMonth() - 12);

time1.setHours(0);

time1.setMinutes(0);

time1.setSeconds(0);

break;

}

//为窗体查询时间变量赋值

searchData.setValueByName("searchtime1", time1.format("YYYY-MM-dd hh:mm:ss"));

searchData.setValueByName("searchtime2", time2.format("YYYY-MM-dd hh:mm:ss")); }

/*

* 时间对象的格式化;

*/

Date.prototype.format = function(format) {

/*

* eg:format="YYYY-MM-dd hh:mm:ss";

*/

var o = {

"M+" : this.getMonth() + 1, //month

"d+" : this.getDate(), //day

"h+" : this.getHours(), //hour

"m+" : this.getMinutes(), //minute

"s+" : this.getSeconds(), //second

"q+" : Math.floor((this.getMonth() + 3) / 3), //quarter

"S" : this.getMilliseconds()

//millisecond

}

if (/(Y+)/.test(format)) {

format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4

- RegExp.$1.length));

}

for (var k in o) {

if (new RegExp("(" + k + ")").test(format)) {

format = format.replace(RegExp.$1, RegExp.$1.length == 1

? o[k]

: ("00" + o[k]).substr(("" + o[k]).length));

}

}

return format;

}

相关主题