惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 叶小钗
D
Darknet – Hacking Tools, Hacker News & Cyber Security
S
SegmentFault 最新的问题
博客园 - 三生石上(FineUI控件)
雷峰网
雷峰网
WordPress大学
WordPress大学
有赞技术团队
有赞技术团队
博客园 - 【当耐特】
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
V
V2EX
V
Visual Studio Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 聂微东
P
Proofpoint News Feed
Last Week in AI
Last Week in AI
U
Unit 42
W
WeLiveSecurity
博客园 - Franky
Recent Announcements
Recent Announcements
Hacker News - Newest:
Hacker News - Newest: "LLM"
Attack and Defense Labs
Attack and Defense Labs
月光博客
月光博客
The Cloudflare Blog
Spread Privacy
Spread Privacy
腾讯CDC
P
Privacy International News Feed
N
News and Events Feed by Topic
AWS News Blog
AWS News Blog
NISL@THU
NISL@THU
T
Troy Hunt's Blog
小众软件
小众软件
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Microsoft Security Blog
Microsoft Security Blog
L
Lohrmann on Cybersecurity
Webroot Blog
Webroot Blog
Y
Y Combinator Blog
量子位
P
Palo Alto Networks Blog
N
News and Events Feed by Topic
V
Vulnerabilities – Threatpost
K
Kaspersky official blog
IT之家
IT之家
T
Threat Research - Cisco Blogs
Cloudbric
Cloudbric
云风的 BLOG
云风的 BLOG
C
Check Point Blog
Blog — PlanetScale
Blog — PlanetScale
爱范儿
爱范儿
G
Google Developers Blog
S
Secure Thoughts

博客园 - 冯岩

RSA算法 Android JAVA C#互通 jquery.autocomplete.js 插件的自定义搜索规则 LINQ TO SQL 动态查询 ASPX多服务器控件下使用Jquery.validate.js 接受来自服务器的数据连接时发生超时(30000 毫秒)问题原因及解决方法 防止网页被客户端IE缓存 dottext阅读之系统调度分析 C#读取被进程占用的文件 SqlServer 无日志文件附加 windows2003远程桌面退出后系统自动注销的解决方法 JS获取触发事件元素的方法 JS处理选取值 Marquee首尾相连不间断移动 开始完全显示 c#将数据导入Excel另类方法 windows Server 2008常见问题及解决方法 .NET 特性Attribute[三] .NET 特性Attribute[二] .NET 特性Attribute[一] [安装程序配置服务器失败]解决SQL Server2000安装失败
Javascript对日期的操作
冯岩 · 2009-02-16 · via 博客园 - 冯岩

javascript对日期处理的常用方法类

<script language="JavaScript">
<!--
//程序:常用公历日期处理程序
 /**//*用相对不规则的字符串来创建日期对象,不规则的含义为:顺序包含年月日三个数值串,有间隔*/
String.prototype.parseDate 
= function(){
    
var ar = (this + ",0,0,0").match(/\d+/g);
    
return ar[5]?(new Date(ar[0],ar[1]-1,ar[2],ar[3],ar[4],ar[5])):(new Date());
}
/**//*
 * 功能:根据输入表达式返回日期字符串
 * 参数:dateFmt:字符串,由以下结构组成    
 *      yy:长写年,YY:短写年mm:数字月,MM:英文月,dd:日,hh:时,
 *      mi:分,ss秒,ms:毫秒,we:汉字星期,WE:英文星期.
 *      isFmtWithZero : 是否用0进行格式化,true or false
 * 返回:对应日期的中文字符串
*/
Date.prototype.toString 
= function(dateFmt,isFmtWithZero){
    dateFmt 
= (dateFmt == null ? "yy-mm-dd hh:mi:ss" : dateFmt);
    
if(typeof(dateFmt) != "string" )
        
throw (new Error(-1'toString()方法的第一个参数为字符串类型!'));
    isFmtWithZero 
= !!isFmtWithZero;
    
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;
    
var o = {
        
"yy" : this.getFullYear(),
        
"YY" : this.getYear(),
        
"mm" : this.getMonth()+1,
        
"MM" : monthArr[this.getMonth()],
        
"dd" : this.getDate(),
        
"hh" : this.getHours(),
        
"mi" : this.getMinutes(),
        
"ss" : this.getSeconds(),
        
"we" : "星期" + weekArr[0][this.getDay()],
        
"WE" : weekArr[1][this.getDay()]
    }
    
for(var i in o){
        str 
= str.replace(new RegExp(i,"g"),o[i].toString().fmtWithZero(isFmtWithZero));
    }
    str 
= str.replace(/ms/g,this.getMilliseconds().toString().fmtWithZeroD(isFmtWithZero));
    
return str;
}
/**//*将一位数字格式化成两位,如: 9 to 09*/
String.prototype.fmtWithZero 
= function(isFmtWithZero){    
    
return (isFmtWithZero && /^\d$/.test(this))?"0"+this:this;
}
String.prototype.fmtWithZeroD 
= function(isFmtWithZero){    
    
return (isFmtWithZero && /^\d{2}$/.test(this))?"00"+this:((isFmtWithZero && /^\d$/.test(this))?"0"+this:this);
}
/**//* 功能 : 返回与某日期相距N天(N个24小时)的日期
 * 参数 : num number类型 可以为正负整数或者浮点数,默认为1;
 *        type 0(秒) or 1(天),默认为天
 * 返回 : 新的Date对象
 
*/
Date.prototype.dateAfter
=function(num,type){
    num 
= (num == null?1:num);
    
if(typeof(num)!="number"throw new Error(-1,"dateAfterDays(num,type)的num参数为数值类型.");
    type 
= (type==null?1:type);
    
var arr = [1000,86400000];
    
return new Date(this.valueOf() + num*arr[type]);
}
//判断是否是闰年,返回true 或者 false
Date.prototype.isLeapYear = function (){
    
var year = this.getFullYear();
    
return (0==year%4 && ((year % 100 != 0)||(year % 400 == 0)));
}
//返回该月天数
Date.prototype.getDaysOfMonth = function (){
    
return (new Date(this.getFullYear(),this.getMonth()+1,0)).getDate();
}
//日期比较函数,参数date:为Date类型,如this日期晚于参数:1,相等:0 早于: -1
Date.prototype.dateCompare = function(date){
    
if(typeof(date) != "object" || !(/Date/.test(date.constructor)))
         
throw new Error(-1,"dateCompare(date)的date参数为Date类型.");
    
var d = this.getTime() - date.getTime();
    
return d>0?1:(d==0?0:-1);
}
/**//*功能:返回两日期之差
 *参数:pd   PowerDate对象
 *    type: 返回类别标识.yy:年,mm:月,ww:周,dd:日,hh:小时,mi:分,ss:秒,ms:毫秒
 *    intOrFloat :返回整型还是浮点型值 0:整型,不等于0:浮点型
 *    output : 输出提示,如:时间差为#周!
 
*/
Date.prototype.calDateDistance 
= function (date,type,intOrFloat,output){
    
if(typeof(date) != "object" || !(/Date/.test(date.constructor)))
         
throw new Error(-1,"calDateDistance(date,type,intOrFloat)的date参数为Date类型.");
    type 
= (type==null?'dd':type);
    
if(!((new RegExp(type+",","g")).test("yy,mm,ww,dd,hh,mi,ss,ms,")))
         
throw new Error(-1,"calDateDistance(pd,type,intOrFloat,output)的type参数为非法.");
    
var iof = (intOrFloat==null?0:intOrFloat);
    
var num=0;
    
var o = {
        
"ww" : 7*86400000,
        
"dd" : 86400000,
        
"hh" : 3600000,
        
"mi" : 60000,
        
"ss" : 1000,
        
"ms" : 1
    }
    
switch(type){
        
case "yy": num = this.getFullYear() - date.getFullYear(); break;
        
case "mm": num = (this.getFullYear() - date.getFullYear())*12+this.getMonth()-date.getMonth(); break;
        
default
            
var sub = this.valueOf() - date.valueOf();
            
if (o[type])
                num 
= (sub/o[type]).fmtRtnVal(iof);
            break;
    }
    
return (output ? output.replace(/#/g," "+num+" ") : num);
}
//返回整数或者两位小数的浮点数
Number.prototype.fmtRtnVal = function (intOrFloat){
    
return (intOrFloat == 0 ? Math.floor(this) : parseInt(this*100)/100);
}
//根据当前日期所在年和周数返回周日的日期
Date.prototype.RtnByWeekNum = function (weekNum){
    
if(typeof(weekNum) != "number")
        
throw new Error(-1,"RtnByWeekNum(weekNum)的参数是数字类型.");
    
var date = new Date(this.getFullYear(),0,1);
    
var week = date.getDay();
    week 
= (week==0?7:week);
    
return date.dateAfter(weekNum*7-week,1);
}
//根据日期返回该日期所在年的周数
Date.prototype.getWeekNum = function (){
    
var dat = new Date(this.getFullYear(),0,1);
    
var week = dat.getDay();
    week 
= (week==0?7:week);
    
var days = this.calDateDistance(dat,"dd")+1;
    
return ((days + 6 - this.getDay() - 7 + week)/7);
}//-->
</script>

<script language="JavaScript">
<!--
var dw = document.write;
var date2 = "2005-12-30".parseDate();
dw(date2,
"<br>");
var date3 = "2006-1-10 11 11 11".parseDate();
dw(date2.calDateDistance(date3,
null,null,"时间差为#天!"));
//alert(new Date(2000,2,-29));
//
-->
</script>