JavaScript之Date操作库

简介:
JavaScript操作Date对象函数库,其中功能有返回本月,上一月,返回本周,上一周,返回本季度,上季度,本年,上一年等.
 
function updateDate(typeName,fromName,toName){
  var today = new Date();
  var ty = document.getElementById(typeName).value;
  var ds=[];
    switch(ty){
        case '': 
            ds[0]='';
            ds[1]='';
            break;    
        case 'today': 
            ds[0]=today.parseString();
            ds[1]=today.parseString();
            break;
        case 'theWeek': 
            ds=today.theWeek();
            break;
        case 'lastWeek':
            ds=today.lastWeek();
            break;
        case 'theMonth':
            ds=today.theMonth();
            break;
        case 'lastMonth':
            ds=today.lastMonth();
            break;
        case 'theQuarter':
            ds=today.theQuarter();
            break;
        case 'lastQuarter':
            ds=today.lastQuarter();
            break;
        case 'theYear':
            ds=today.theYear();
            break;
        case 'lastYear':
            ds=today.lastYear();
            break;
        case 'last7Day':
            ds=today.lastDays(7);
            break;
        case 'last30Day':
            ds=today.lastDays(30);
            break;
        case 'custom':
            ds[0]='';
            ds[1]='';
            break;            
    }
 if (ds!=null){
    document.getElementById(fromName).value=ds[0];
    document.getElementById(toName).value=ds[1];
    }
}

function updateType(typeName){
    document.getElementById(typeName).selectedIndex=12;
}

//返
Date.prototype.getDaysOfMonth = function (){
    return (new Date(this.getFullYear(),this.getMonth()+1,0)).getDate();
}

//上
Date.prototype.lastQuarter = function(){
    var arr= [];
 ar m = this.getMonth();
    var q = Math.floor(m/3)+1;//本
    
    var f;//开
    var l;//结
    switch(q){
        case 1: 
            f=new Date(this.getFullYear()-1,9,1);
            l=new Date(this.getFullYear()-1,11,31);
            break;
        case 2: 
            f=new Date(this.getFullYear(),0,1);
            l=new Date(this.getFullYear(),2,31);
            break;
        case 3: 
            f=new Date(this.getFullYear(),3,1);
            l=new Date(this.getFullYear(),5,30);
            break;
        case 4: 
            f=new Date(this.getFullYear(),6,1);
            l=new Date(this.getFullYear(),8,30);
            break;
    }

    arr[0]=f.parseString();    
    arr[1]=l.parseString();
    return arr;
}
//本
Date.prototype.theQuarter = function(){
    var arr= [];
 ar m = this.getMonth();
    var q = Math.floor(m/3)+1;//季
    
    var t=new Date(this.getFullYear(),3*q-2-1,1);
    arr[0]=t.parseString();    
    arr[1]=this.parseString();
    return arr;
}



//去
Date.prototype.lastYear = function(){
    var arr= [];
    var t = new Date(this.getFullYear()-1,0,1);//去
    var l= new Date(this.getFullYear()-1,11,31);//去

    arr[0]=t.parseString();    
    arr[1]=l.parseString();  
    return arr;
}

//今
Date.prototype.theYear = function(){
    var arr= [];
    var t = new Date(this.getFullYear(),0,1);
    arr[0]=t.parseString();    
    arr[1]=this.parseString(); 
    return arr;
}
//最
Date.prototype.lastDays = function(num){
    var arr= [];
    arr[0]=this.dateAfter(-num).parseString();    
    arr[1]=this.parseString();
    return arr;
}

//上
Date.prototype.lastMonth = function(){
    var arr= [];
    var t = new Date(this.getFullYear(),this.getMonth(),1);//本
    var l= t.dateAfter(-1);//上
    var f = new Date(l.getFullYear(),l.getMonth(),1);//上

    arr[0]=f.parseString();    
    arr[1]=l.parseString(); //上
    return arr;
}


//本
Date.prototype.theMonth = function(){
    var arr= [];
    var d=this.getDate();
    arr[0]=this.dateAfter(-d+1).parseString();
    arr[1]=this.parseString();
    return arr;
}

//本
Date.prototype.theWeek = function(){
    var arr= [];
    var w=this.getDay();
    if (w==0) w=7;
    arr[0]=this.dateAfter(-w).parseString();
    arr[1]=this.parseString();
    return arr;
}

//上
Date.prototype.lastWeek = function(){
    var arr= [];
    var w=this.getDay();
    if (w==0) w=7;
    arr[0]=this.dateAfter(-w-7).parseString();
    arr[1]=this.dateAfter(-w).parseString();
    return arr;
}

/*将如 9 to 09*/
String.prototype.fmtWithZero = function(isFmtWithZero){
if(isFmtWithZero)
return (this<10?"0"+this:this);
else return this;
}
String.prototype.fmtWithZeroD = function(isFmtWithZero){
if(isFmtWithZero)
return (this<10?"00"+this:(this<100?"0"+this:this));
else return this;
}
/*
* 功根
* 参dateFmt:字由
* yy:长YY:短m:数MM:英dd:日hh:时
* mi:分ss秒ms:毫we:汉WE:英
* isFmtWithZero : 是进true or false
*/
Date.prototype.parseString = function(dateFmt,isFmtWithZero){
dateFmt = (dateFmt == null?"yy-mm-dd" : dateFmt);
isFmtWithZero = (isFmtWithZero == null?true : isFmtWithZero);
if(typeof(dateFmt) != "string" )
throw (new Error(-1, 'parseString()方'));
var weekArr=[["星,"星,"星,"星,"星,"星,"星],
["SUN","MON","TUR","WED","THU","FRI","SAT"]];
var monthArr=["JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"];
var str=dateFmt;
str = str.replace(/yy/g,this.getFullYear());
str = str.replace(/YY/g,this.getYear());
str = str.replace(/mm/g,(this.getMonth()+1).toString().fmtWithZero(isFmtWithZero));
str = str.replace(/MM/g,monthArr[this.getMonth()]);
str = str.replace(/dd/g,this.getDate().toString().fmtWithZero(isFmtWithZero));
str = str.replace(/hh/g,this.getHours().toString().fmtWithZero(isFmtWithZero));
str = str.replace(/mi/g,this.getMinutes().toString().fmtWithZero(isFmtWithZero));
str = str.replace(/ss/g,this.getSeconds().toString().fmtWithZero(isFmtWithZero));
str = str.replace(/ms/g,this.getMilliseconds().toString().fmtWithZeroD(isFmtWithZero));
str = str.replace(/we/g,weekArr[0][this.getDay()]);
str = str.replace(/WE/g,weekArr[1][this.getDay()]);
return str;
}

 //格006-01-01的
 Date.prototype.formatString = function(){
   var s='';
   s+=this.getFullYear()+"-"; 
   s +=(this.getMonth()+1)+"-";   
   s+=this.getDate();        
   return s;
 }

 Date.prototype.minusDays = function(days){
     var interTimes = days * 24 * 60 * 60 * 1000 ;
     return new Date(Date.parse(this) - interTimes) ;     
 }

 
/* 功: 返天N个4小的
* 参: num number类可默;
* type 0(秒 or 1(天,默
* 返: 新owerDate类
*/
Date.prototype.dateAfter=function(num,type){
num = (num == null?1:num);
if(typeof(num)!="number") throw new Error(-1,"dateAfterDays(num,type)的um参");
type = (type==null?1:type);
var arr = [1000,86400000];
var dd = this.valueOf();
dd += num*arr[type];
return new Date(dd);
}

 

延伸阅读:Javascript之Date操作

Sam Lin 标签:  JavaScriptDate
转载请注明出处[ http://samlin.cnblogs.com/] 
作者赞赏
 


刚做的招标网: 八爪鱼招标网 请大家多意见
标签:  JavaScriptDate

本文转自Sam Lin博客博客园博客,原文链接:http://www.cnblogs.com/samlin/archive/2009/01/14/JavaScript-Date-Library.html,如需转载请自行联系原作者
目录
相关文章
|
1月前
|
JavaScript 前端开发
JavaScript操作DOM元素
JavaScript操作DOM元素
12 1
|
1月前
|
JavaScript 前端开发
如何使用 JavaScript 操作 DOM?
如何使用 JavaScript 操作 DOM?
13 0
|
1月前
|
前端开发 JavaScript 区块链
连接区块链节点的 JavaScript 库 web3.js
连接区块链节点的 JavaScript 库 web3.js
27 2
|
1月前
|
移动开发 前端开发 JavaScript
编程笔记 html5&css&js 005 网页上都有哪内容、形式和操作
编程笔记 html5&css&js 005 网页上都有哪内容、形式和操作
|
2月前
|
JavaScript 数据处理
JS 取整,取余操作
JS 取整,取余操作
|
4天前
|
JavaScript 前端开发 UED
深入解析JavaScript原生操作DOM技术
【4月更文挑战第22天】本文深入探讨JavaScript原生DOM操作技术,包括使用`getElement*`方法和CSS选择器获取元素,借助`createElement`与`appendChild`动态创建及插入元素,修改元素内容、属性和样式,以及删除元素。通过掌握这些技术,开发者能实现页面动态交互,但应注意避免过度操作DOM以优化性能和用户体验。
|
7天前
|
算法
Swiper库和Glide.js库的性能有何区别
Swiper和Glide.js是两个流行的响应式轮播图库。Swiper功能强大且灵活,支持多方向滑动,拥有丰富的配置和切换效果,适合复杂需求,其高性能得益于优化的算法和惰性加载。Glide.js则轻量级、快速,专注于基础功能,适合简洁需求。两者各有侧重,选择应基于项目具体需求和性能考虑。
|
10天前
|
JavaScript
【Js】检查Date对象是否为Invalid Date
【Js】检查Date对象是否为Invalid Date
14 0
|
11天前
|
存储 JavaScript 前端开发
JavaScript DOM 操作:解释一下 cookie、sessionStorage 和 localStorage 的区别。
Cookie是服务器发送至客户端的文本信息,会随每个请求发送回服务器,适合控制会话状态但可能暴露隐私。SessionStorage仅在当前会话中存储数据,关闭浏览器后清除,适合临时存储如登录状态。LocalStorage则持久保存数据,即使关闭浏览器也不会清除,适用于存储长期设置。三种方式各有侧重,应按需求选择。
15 0
|
11天前
|
JavaScript 前端开发 安全
JavaScript DOM 操作:解释一下浏览器的同源策略。
**同源策略**是浏览器安全基石,它阻止脚本跨不同协议、域名或端口访问资源,防止恶意行为。例如,HTTP页面无法直接用JS获取HTTPS页面内容。**CORS**允许跨域请求,但需服务器配合设置,通过`document.domain`属性可配置,但仍受限于服务器配置。
14 4