javascript常见操作汇总常见操作汇总
本文整理汇总了javascript常见的各类操作,包括字符串、时间、表单、正则验证等等。有着极高的参考价值。分享给大家供
大家参考之用。具体方法如下:
/***** BasePage.js 公共的 脚本文件 部分方法需引用jquery库 *****/
//#region 日期操作
//字符串转化为时间。
function stringtoTime(date1) {
var dt = new Date(Date.parse(date1.replace(/-/g, "/")));
return dt;
}
// 使用 var date1 = "2013-06-08 15:23:31"或"2013/6/8 9:9:00"格式;
//-------------------------------------------------------------
// 日期格式化
Date.prototype.format = function (format) {
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;
}
////使用方法
//alert(newdate.format("yyyy-MM-dd"));
//alert(newdate.format("MM/dd/yyyy"));
//var nowStr = now.format("yyyy-MM-dd hh:mm:ss"); Math.round(Math.random() * 10000)
////使用方法2:
//var testDate = new Date();
//var testStr = testDate.format("yyyy年MM月dd日hh小时mm分ss秒");
////示例:
//alert(testDate.format("yyyy年MM月dd日"));
//-------------------------------------------------------------
//设置周期内的日期(数组)
function SetFlag(start, end) {
var cdate = Array();
cdate = start.split("-");
var cd = cdate[1] + "/" + cdate[2] + "/" + cdate[0];
var dayNum = DateDiff(end, start);
for (var i = 0; i <= dayNum; i++) {
flag.push(AddDays(cd, i));
}
} //end fun
//日期加上天数后的新日期.
function AddDays(date, days) {
var nd = new Date(date);
nd = nd.valueOf();
nd = nd + days * 24 * 60 * 60 * 1000;