JS日期处理

一.日期转换

将字符串形式的日期转换成日期对象

var strTime="2011-04-16"; //字符串日期格式            
var date= new Date(Date.parse(strTime.replace(/-/g,   "/"))); //转换成Data();

var month=date.getMonth()+1; //获取当前月份

------------------------------------------------------------------------------------------------------

date.getYear();        //获取当前年份(2位)
date.getFullYear();    //获取完整的年份(4位,1970-????)
date.getMonth();       //获取当前月份(0-11,0代表1月)
date.getDate();        //获取当前日(1-31)
date.getDay();         //获取当前星期X(0-6,0代表星期天)
date.getTime();        //获取当前时间(从1970.1.1开始的毫秒数)
date.getHours();       //获取当前小时数(0-23)
date.getMinutes();     //获取当前分钟数(0-59)
date.getSeconds();     //获取当前秒数(0-59)
date.getMilliseconds();    //获取当前毫秒数(0-999)
date.toLocaleDateString();     //获取当前日期
var mytime=date.toLocaleTimeString();     //获取当前时间
date.toLocaleString( );        //获取日期与时间

 --------------------------------------------------------------------------------------------------------

1.当前系统区域设置格式(toLocaleDateString和toLocaleTimeString) 

例子:(new Date()).toLocaleDateString() + " " + (new Date()).toLocaleTimeString()
结果: 2008年1月29日 16:13:11

2.普通字符串(toDateString和toTimeString) 

例子: (new Date()).toDateString() + " " + (new Date()).toTimeString()
结果:Tue Jan 29 2008 16:13:11 UTC+0800

3.格林威治标准时间(toGMTString) 

例子: (new Date()).toGMTString()
结果:Tue, 29 Jan 2008 08:13:11 UTC

4.全球标准时间(toUTCString) 

例子: (new Date()).toUTCString()
结果:Tue, 29 Jan 2008 08:13:11 UTC

5.Date对象字符串(toString) 

例子: (new Date()).toString()
结果:Tue Jan 29 16:13:11 UTC+0800 2008

 

二.日期相加

js日期相加

// 增加天
function AddDays(date,value)
{
date.setDate(date.getDate()+value);
}

// 增加月
function AddMonths(date,value)
{
date.setMonth(date.getMonth()+value);
}

// 增加年
function AddYears(date,value)
{
date.setFullYear(date.getFullYear()+value);
}

// 是否为今天
function IsToday(date)
{
return IsDateEquals(date,new Date());
}

// 是否为当月
function IsThisMonth(date)
{
return IsMonthEquals(date,new Date());
}

// 两个日期的年是否相等
function IsMonthEquals(date1,date2)
{
return date1.getMonth()==date2.getMonth()&&date1.getFullYear()==date2.getFullYear();
}

// 判断日期是否相等
function IsDateEquals(date1,date2)
{
return date1.getDate()==date2.getDate()&&IsMonthEquals(date1,date2);
}

// 返回某个日期对应的月份的天数
function GetMonthDayCount(date)
{
switch(date.getMonth()+1)
{
case 1:case 3:case 5:case 7:case 8:case 10:case 12:
return 31;
case 4:case 6:case 9:case 11:
return 30;
}
//feb:
date=new Date(date);
var lastd=28;
date.setDate(29);
while(date.getMonth()==1)
{
lastd++;
AddDays(date,1);
}
return lastd;
}

// 返回两位数的年份
function GetHarfYear(date)
{
var v=date.getYear();
if(v>9)return v.toString();
return "0"+v;
}

// 返回月份(修正为两位数)
function GetFullMonth(date)
{
var v=date.getMonth()+1;
if(v>9)return v.toString();
return "0"+v;
}

// 返回日 (修正为两位数)
function GetFullDate(date)
{
var v=date.getDate();
if(v>9)return v.toString();
return "0"+v;
}

// 替换字符串
function Replace(str,from,to)
{
return str.split(from).join(to);
}

// 格式化日期的表示
function FormatDate(date,str)
{
str=Replace(str,"yyyy",date.getFullYear());
str=Replace(str,"MM",GetFullMonth(date));
str=Replace(str,"dd",GetFullDate(date));
str=Replace(str,"yy",GetHarfYear(date));
str=Replace(str,"M",date.getMonth()+1);
str=Replace(str,"d",date.getDate());
return str;
}

// 统一日期格式
function ConvertDate(str)
{
str=(str+"").replace(/^\s*/g,"").replace(/\s*$/g,""); // 去除前后的空白
var d;
if(/^[0-9]{8}$/.test(str)) // 20040226 -> 2004-02-26
{
d=new Date(new Number(str.substr(0,4)),new Number(str.substr(4,2))-1,new Number(str.substr(6,2)));
if(d.getTime())return d;
}
d=new Date(str);
if(d.getTime())return d;
d=new Date(Replace(str,"-","/"));
if(d.getTime())return d;
return null;
}   

js时间差函数
<SCRIPT LANGUAGE=javascript>
alert(addDay(-30,1));
alert(addDay(-30,2));
alert(addDay(-30,3));
alert(addDay(-30,0));

function addDay(days,n)
{
//函数说明:days日期差,n代表如下含义。
var my_date_ago=new Date(new Date() - days * 24 * 60 * 60 * 1000 * -1);//days天的日期
switch (n)
{
case 1:
//返回年
return(my_date_ago.getFullYear());
break;
case 2:
//返回月
return(my_date_ago.getMonth()+1);
break;
case 3:
//返回日
return(my_date_ago.getDate());
break;
default :
//返回全部
return(my_date_ago.getFullYear() + "-" + (my_date_ago.getMonth()+1) + "-" + my_date_ago.getDate());
break;
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值