JS 处理日期几个方法

function getCurrentDate(){
var today = new Date();
var day = today.getDate();
var month = today.getMonth() + 1;
var year = today.getYear();

if(year<1900) year += 1900;

//The value returned by getYear is the year less 1900. For example,
//if the year is 1976, the value returned is 76.

hour = today.getHours();
minute = today.getMinutes();
second = today.getSeconds();
day=day-1;
if(day==0)
{
month=month-1;
day=getMaxDay(year,month)
}
if(day<10) day = '0'+day;
if(month<10) month = '0'+month;


return year+"-"+month+"-"+day
}


/**
* 用途:获取每月的最后一天日期值.
* 输入:date:年份;month:月份
* 返回:如果通过验证返回true,否则返回false
*/
function getMaxDay(year,month) {
if(month==04||month==06||month==09||month==11)
return "30";
if(month==02)
if(year%4==0&&year%100!=0 || year%400==0)
return "29";
else
return year+"-"+month+"-"+"28";
return "31";
}


==========================================================
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
<SCRIPT LANGUAGE="JavaScript">
function getCurrentDate(){
var today = new Date();
var day = today.getDate();
var month = today.getMonth() + 1;
var year = today.getYear();

if(year<1900) year += 1900;

//The value returned by getYear is the year less 1900. For example,
//if the year is 1976, the value returned is 76.

hour = today.getHours();
minute = today.getMinutes();
second = today.getSeconds();
day=day-1;
if(day==0)
{
month=month-1;
day=getMaxDay(year,month)
}
if(day<10) day = '0'+day;
if(month<10) month = '0'+month;



return year+"-"+month+"-"+day
}
</SCRIPT>
</HEAD>

<BODY>
<button value="当前日期" onclick="alert(getCurrentDate())">currentDate</button>
</BODY>
</HTML>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值