js 日期函数用法总结

1 创建Date对象,用于处理日期和时间

var date=new Date();

Date对象会把当前日期和时间保存为初始值。

还可以设置其它参数初始化 Date对象:

new Date("month dd,yyyy hh:mm:ss");
new Date("month dd,yyyy");
new Date(yyyy,mth,dd,hh,mm,ss);
new Date(yyyy,mth,dd);
new Date(ms);

new Date("yyyy/MM/dd");

参数说明:

month:用英文表示月份名称,从January到December

mth:用整数表示月份,从0-11(1月到12月)

dd:表示一个月中的第几天,从1到31

yyyy:四位数表示的年份

hh:小时数,从0(午夜)到23(晚11点)

mm:分钟数,从0到59的整数

ss:秒数,从0到59的整数

ms:毫秒数,为大于等于0的整数

如:

new Date("January 12,2006 22:19:35");

new Date("January 12,2006");

new Date(2006,0,12,22,19,35);

new Date(2006,0,12);

new Date(1137075575000);

new Date("2014/09/22");

2. Date 对象的方法

              日期访问方法:

Method(方法)

Description (描述)

Value(数值)

getYear()

返回年份的最后两位数字

2001

getMonth()

返回年份中的第几月(0到11)

5

getDate()

返回月份中的第几日(1到31)

2

getDay()

返回星期中的第几天 (0到6)

6

getTimezoneOffset()

返回当地时间与格林尼治天文台平均时间的差别

-480 (-8h)

getHours()

返回一天中的时针点数(0到23)

16

getMinutes()

返回分钟 (0..59)

8

getSeconds()

返回时间的秒 (0到59)

24

getTime()

返回自从公元1970年1月1日的毫秒数

991469304470

日期设置方法

setDate()

设置每月中的第几天(从0到30)

setHours()

设置小时(从0到23)

setMinutes()

设置分钟(从0到59)

setMonth()

设置月份(从0到11)

setSeconds()

设置秒数(从0到59)

setTime()

设置时间(从公元以来的毫秒数)

setYear()

设置年份

其它的日期方法:

parse

转化日期字符串为自从公元以来的毫秒数,比如Date.parse(“date string”)

toString()

Sat Jun 2 16:08:24 UTC+0800 2001

toGMTString()

Sat, 2 Jun 2001 08:08:24 UTC

toLocaleString()

2001年6月2日 16:08:24

应用例子:使用getDay()方法获取星期数

var myDate=new Date();
var weekCn=["星期日","星期一","星期二","星期三","星期四","星期五","星期六"];
alert(weekCn[myDate.getDay()]);

3  通过扩展方法format格式化 Date

// 对Date的扩展,将 Date 转化为指定格式的String
// 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符, 
// 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字) 
// 例子: 
// (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423 
// (new Date()).Format("yyyy-M-d h:m:s.S")      ==> 2006-7-2 8:9:4.18 
Date.prototype.Format = function (fmt) { //author: meizz 
    var o = {
        "M+": this.getMonth() + 1, //月份 
        "d+": this.getDate(), //
        "h+": this.getHours(), //小时 
        "m+": this.getMinutes(), //
        "s+": this.getSeconds(), //
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度 
        "S": this.getMilliseconds() //毫秒 
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}

调用:
new Date().format('yyyy-MM-dd');

其它实用方法:

//函数名:CheckDateTime      
//功能介绍:检查是否为日期  
function  CheckDate(str){                            
       var  reg  =  /^(d+)-(d{1,2})-(d{1,2})$/;    
       var  r  =  str.match(reg);    
       if(r==null)return  false;    
       r[2]=r[2]-1;    
       var  d=  new  Date(r[1],  r[2],r[3]);    
       if(d.getFullYear()!=r[1])return  false;    
       if(d.getMonth()!=r[2])return  false;    
       if(d.getDate()!=r[3])return  false;    
  
       return  true;  
}

调用:
alert(CheckDate("2014-9-23"))  
原文地址:https://www.cnblogs.com/jingping/p/3987450.html