/輸入時(shí)間轉(zhuǎn)換
function formatTime(date) {
var year = date.getFullYear()
var month = date.getMonth() + 1
var day = date.getDate()
var hour = date.getHours()
var minute = date.getMinutes()
var second = date.getSeconds()
return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}
//輸入時(shí)間和輸出形式
function formattime(number, format) {
if (number != null) {
var formateArr = ['Y', 'M', 'D', 'h', 'm', 's'];//
var returnArr = [];
var date = new Date(number);
returnArr.push(date.getFullYear());
returnArr.push(formatNumber(date.getMonth() + 1));
returnArr.push(formatNumber(date.getDate()));
returnArr.push(formatNumber(date.getHours()));
returnArr.push(formatNumber(date.getMinutes()));
returnArr.push(formatNumber(date.getSeconds()));
for (var i in returnArr) {
format = format.replace(formateArr[i], returnArr[i]);
}
//format.replace(/\//g,'-');
return format.replace(/\//g, '-');
} else {
return number;
}
}
//formatNumber函數(shù),數(shù)字格式
function formatNumber(n) {
n = n.toString()
return n[1] ? n : '0' + n
}
//判斷輸入的手機(jī)號(hào)是否正確。
function regexConfig() {
var reg = {
phone: /^1(3|4|5|7|8)\d{9}$/ //手機(jī)號(hào)匹配規(guī)則
}
return reg;
}
// 去前后空格
function trim(str) {
return str.replace(/(^\s*)|(\s*$)/g, "");
}
//判斷是否為數(shù)字
function isNumber(str){
var n = Number(str);
if (!isNaN(n)){
return true;
}else{
return false;
}
}
//判斷是否對(duì)象
function isObj(obj){
if(typeof obj=="object"){
return true;
}else{
return false;
}
}
//判斷是否為空
function emptyFun(obj){
var obj=obj;
if(obj==""||obj==null||obj==undefined||obj=="null"||obj=="undefined"){
return true;
}else{
return false;
}
}
//獲取地址欄參數(shù)
function GetQueryString(name){
var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
var r = window.location.search.substr(1).match(reg);
if(r!=null)return unescape(r[2]); return null;
}
//轉(zhuǎn)換時(shí)間格式 eg:1972-10-26
function getLocalTime(nS) {
var now = new Date(nS);
if(!now){
return 0;
}else{
var year=now.getFullYear();//年
var month=now.getMonth()+1;//月
var date=now.getDate();//日
var hour=now.getHours();//時(shí)
var minute=now.getMinutes();//分
var second=now.getSeconds();//秒
return year+"-"+month+"-"+date;
}
}
//數(shù)組去重
Array.prototype.unique1 = function(){
var res = [this[0]];
for(var i = 1; i < this.length; i++){
var repeat = false;
for(var j = 0; j < res.length; j++){
if(this[i] == res[j]){
repeat = true;
break;
}
}
if(!repeat){
res.push(this[i]);
}
}
return res;
}
//校驗(yàn)是否全由數(shù)字組成
function isDigit(s)
{
var patrn=/^[0-9]{1,20}$/;
if (!patrn.exec(s)) return false
return true
}
//校驗(yàn)登錄名:只能輸入5-20個(gè)以字母開頭、可帶數(shù)字、“_”、“.”的字串
function isRegisterUserName(s)
{
var patrn=/^[a-zA-Z]{1}([a-zA-Z0-9]|[._]){4,19}$/;
if (!patrn.exec(s)) return false
return true
}
//校驗(yàn)用戶姓名:只能輸入1-30個(gè)以字母開頭的字串
function isTrueName(s)
{
var patrn=/^[a-zA-Z]{1,30}$/;
if (!patrn.exec(s)) return false
return true
}
//校驗(yàn)密碼:只能輸入6-20個(gè)字母、數(shù)字、下劃線
function isPasswd(s)
{
var patrn=/^(\w){6,20}$/;
if (!patrn.exec(s)) return false
return true
}
//校驗(yàn)普通電話、傳真號(hào)碼:可以“+”開頭,除數(shù)字外,可含有“-”
function isTel(s)
{
//var patrn=/^[+]{0,1}(\d){1,3}[ ]?([-]?(\d){1,12})+$/;
var patrn=/^[+]{0,1}(\d){1,3}[ ]?([-]?((\d)|[ ]){1,12})+$/;
if (!patrn.exec(s)) return false
return true
}
//檢測(cè)IP地址
function isIP(s)
{
var patrn=/^[0-9.]{1,20}$/;
if (!patrn.exec(s)) return false
return true
}
//轉(zhuǎn)換時(shí)間 eg:1972/10/26 上午3:21
function getLocalTime(nS) {
if(!nS){
return 0;
}else{
return new Date(parseInt(nS)).toLocaleString().replace(/:\d{1,2}$/,' ');
}
}
//對(duì)象排序
var dic={x:2,z:1,y:3};//輸出 {z:1,x:2,y:3}
var sdic=Object.keys(dic).sort(function(a,b){return dic[a]-dic[b]});
for(ki in sdic){
console.log(sdic[ki]+":"+dic[sdic[ki]]+",");
}
// js sort方法根據(jù)數(shù)組中對(duì)象的某一個(gè)屬性值進(jìn)行排序
function keysrt(key,desc) {
return function(a,b){
return desc ? ~~(a[key] < b[key]) : ~~(a[key] > b[key]);
}
}
使用:
var ary=[{id:1,name:"b"},{id:2,name:"b"}];
ary.sort(keysrt('name',true));
//校驗(yàn)手機(jī)號(hào)碼:必須以數(shù)字開頭,除數(shù)字外,可含有“-”
function isMobil(s)
{
var patrn=/^[+]{0,1}(\d){1,3}[ ]?([-]?((\d)|[ ]){1,12})+$/;
if (!patrn.exec(s)) return false
return true
}
//校驗(yàn)郵政編碼
function isPostalCode(s)
{
var patrn=/^[a-zA-Z0-9 ]{3,12}$/;
if (!patrn.exec(s)) return false
return true
}
//校驗(yàn)搜索關(guān)鍵字
function isSearch(s)
{
var patrn=/^[^`~!@#$%^&*()+=|\\\][\]\{\}:;\'\,.<>/?]{1}[^`~!@$%^&()+=|\\\][\]\{\}:;\'\,.<>?]{0,19}$/;
if (!patrn.exec(s)) return false
return true
}