javascript-日期时间

jeDate插件

var myDate = new Date();
// 日期插件
jeDate("#inputDate", {
format: "YYYY-MM-DD",
isTime: false,
minDate: "2014-09-19 00:00:00",
isinitVal:true,
initDate:[{YYYY:myDate.getFullYear(),MM:myDate.getMonth(),DD:myDate.getDate()},false],
})

获取当前日期

// 对Date的扩展,将 Date 转化为指定格式的String
// 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符,
// 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
// 例子:
// (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423
// (new Date()).Format("yyyy-M-d h:m:s.S") ==> 2006-7-2 8:9:4.18

Date.prototype.Format = function (fmt) { // author: meizz
var o = {
"M+": this.getMonth() + 1, // 月份
"d+": this.getDate(), // 日
"h+": this.getHours(), // 小时
"m+": this.getMinutes(), // 分
"s+": this.getSeconds(), // 秒
"q+": Math.floor((this.getMonth() + 3) / 3), // 季度
"S": this.getMilliseconds() // 毫秒
};
if (/(y+)/.test(fmt))
fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var k in o)
if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
return fmt;
}

字符串转时间对象

var sDate1 = "2008/04/02";
var oDate1 = new Date(sDate1);

时间减法

oDate1.setHours(oDate1.getHours() - 1);

//小时减法 subHours(start,1);
function subHours(start, subVal) {
return new Date(start.getTime() - subVal * 3600*1000);
}

日期时间对象格式化


/*
Based on Rick Strahl code
http://www.west-wind.com/weblog/posts/2008/Mar/18/A-simple-formatDate-function-for-JavaScript

Contributors:
Clauber Stipkovic - @clauberhalic
Mário Rinaldi - @MarioRinaldi

对Date的扩展,将 Date 转化为指定格式的String
月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符,
年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
例子:
(new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423
(new Date()).Format("yyyy-M-d h:m:s.S") ==> 2006-7-2 8:9:4.18

*/
Date.prototype.Format = function (format, utc) {
var date = this,
day = utc ? date.getUTCDate() : date.getDate(),
month = (utc ? date.getUTCMonth() : date.getMonth()) + 1,
year = utc ? date.getUTCFullYear() : date.getFullYear(),
hours = utc ? date.getUTCHours() : date.getHours(),
minutes = utc ? date.getUTCMinutes() : date.getMinutes(),
seconds = utc ? date.getUTCSeconds() : date.getSeconds();
if (!format) {
format = "MM/dd/yyyy";
}
format = format.replace("MM", month.toString().replace(/^(\d)$/, '0$1'));
if (format.indexOf("yyyy") > -1) {
format = format.replace("yyyy", year.toString());
} else if (format.indexOf("yy") > -1) {
format = format.replace("yy", year.toString().substr(2, 2));
}
format = format.replace("dd", day.toString().replace(/^(\d)$/, '0$1'));
if (format.indexOf("t") > -1) {
if (hours > 11) {
format = format.replace("t", "pm");
} else {
format = format.replace("t", "am");
}
}
if (format.indexOf("HH") > -1) {
format = format.replace("HH", hours.toString().replace(/^(\d)$/, '0$1'));
}
if (format.indexOf("hh") > -1) {
// if (hours > 12) {
// hours -= 12;
// }
// if (hours === 0) {
// hours = 12;
// }
format = format.replace("hh", hours.toString().replace(/^(\d)$/, '0$1'));
}
if (format.indexOf("mm") > -1) {
format = format.replace("mm", minutes.toString().replace(/^(\d)$/, '0$1'));
}
if (format.indexOf("ss") > -1) {
format = format.replace("ss", seconds.toString().replace(/^(\d)$/, '0$1'));
}
return format;
};

playStart = oDate1.Format("yyMMddhhmmss");
var aa= new Date().Format("yyyy-MM-dd hh:mm:ss");

var oDate1 = new Date(lastActiveDatetime);
var playEnd = oDate1.Format("yyMMddhhmmss", false);

timer