日期常用函数

var now = new Date();                    //当前日期
var nowDayOfWeek = now.getDay();         //今天本周的第几天
var nowDay = now.getDate();              //当前日
var nowMonth = now.getMonth();           //当前月
var nowYear = now.getYear();             //当前年
nowYear += (nowYear < 2000) ? 1900 : 0;  //

//格式化日期:yyyy-MM-dd
function formatDate(date) {
var myyear = date.getFullYear();
var mymonth = date.getMonth()+1;
var myweekday = date.getDate();

if(mymonth < 10){
mymonth = "0" + mymonth;
}
if(myweekday < 10){
myweekday = "0" + myweekday;
}
return (myyear+"-"+mymonth + "-" + myweekday);
}

//获得某月的天数
function getMonthDays(myMonth){
var monthStartDate = new Date(nowYear, myMonth, 1);
  var monthEndDate = new Date(nowYear, myMonth + 1, 1);
  var   days   =   (monthEndDate   -   monthStartDate)/(1000   *   60   *   60   *   24);
    return   days;
}

//获得本季度的开始月份
function getQuarterStartMonth(){
var quarterStartMonth = 0;
if(nowMonth<3){
   quarterStartMonth = 0;
}
if(2<nowMonth && nowMonth<6){
   quarterStartMonth = 3;
}
if(5<nowMonth && nowMonth<9){
   quarterStartMonth = 6;
}
if(nowMonth>8){
   quarterStartMonth = 9;
}
return quarterStartMonth;
}

//获得本周的开始日期
function getWeekStartDate() {
var weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek);
return formatDate(weekStartDate);
}

//获得本周的结束日期
function getWeekEndDate() {
var weekEndDate = new Date(nowYear, nowMonth, nowDay + (6 - nowDayOfWeek));
return formatDate(weekEndDate);
}

//获得本月的开始日期
function getMonthStartDate(){
var monthStartDate = new Date(nowYear, nowMonth, 1);
return formatDate(monthStartDate);
}

//获得本月的结束日期
function getMonthEndDate(){
  var monthEndDate = new Date(nowYear, nowMonth, getMonthDays(nowMonth));
return formatDate(monthEndDate);
}

//获得本季度的开始日期
function getQuarterStartDate(){

var quarterStartDate = new Date(nowYear, getQuarterStartMonth(), 1);
return formatDate(quarterStartDate);
}

//或的本季度的结束日期
function getQuarterEndDate(){
var quarterEndMonth = getQuarterStartMonth() + 2;
var quarterStartDate = new Date(nowYear, quarterEndMonth, getMonthDays(quarterEndMonth));
return formatDate(quarterStartDate);
}





//计算时间差 yyyy-MM-dd HH:mm:ss
function countDate(startDateStr,endDateStr){
if(startDateStr==''||endDateStr==''){
return 0;
}
//开始时间
var strArrs = startDateStr.split(" ");
var strDays = strArrs[0].split("-");
var strTimes = strArrs[1].split(":");
var yys = strDays [0];
var mms = strDays [1];
var dds = strDays [2];
var BHour = strTimes[0];
var BM = strTimes[1];
var ses = strTimes[2];
//结束时间
var strArre = endDateStr.split(" ");
var strDaye = strArre[0].split("-");
var strTimee = strArre[1].split(":");
var yye = strDaye [0];
var mme = strDaye [1];
var dde = strDaye [2];
var EHour = strTimee[0];
var EM = strTimee[1];
var see = strTimee[2];

var start = new Date(Date.UTC(yys, mms-1, dds, BHour-8, BM, ses));
var end = new Date(Date.UTC(yye, mme-1, dde, EHour-8, EM, see));
var timenow=(end.getTime()-start.getTime())/(24*60*60*1000);
//alert(timenow/(24*60*60*1000));
//保留2为小数
var dayNow=timenow.toFixed(2);
//保留整数
//var dayNow=parseInt((end-start)/(24*60*60*1000));
return dayNow;

}
//时间差
function TimeComplete(bHour,bMin,eHour,eMin)
{
var BTime="2007/1/1 "+bHour+":"+bMin;
var ETime="2007/1/1 "+eHour+":"+eMin;
var d=new Date(BTime);
var d2=new Date(ETime);
var hSpan=d2.getTime()-d.getTime();
return hSpan;

}


//日期格式化
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;
}

//使用方法
var now = new Date();
var nowStr = now.format("yyyy-MM-dd hh:mm:ss");
//使用方法2:
var testDate = new Date();
var testStr = testDate.format("YYYY年MM月dd日hh小时mm分ss秒");
alert(testStr);
//示例:
alert(new Date().Format("yyyy年MM月dd日"));
alert(new Date().Format("MM/dd/yyyy"));
alert(new Date().Format("yyyyMMdd"));
alert(new Date().Format("yyyy-MM-dd hh:mm:ss"));

详细出处参考:http://www.jb51.net/article/22657.htm



你可能感兴趣的:(常用函数)