您的位置:首页 > Web前端 > JavaScript

js 取年月日与年月日加减

2014-03-25 16:27 423 查看

<script language="javascript">
//取值
var myDate = new Date();
myDate.getYear(); //获取当前年份(2位)
myDate.getFullYear(); //获取完整的年份(4位,1970-????)
myDate.getMonth(); //获取当前月份(0-11,0代表1月)
myDate.getDate(); //获取当前日(1-31)
myDate.getDay(); //获取当前星期X(0-6,0代表星期天)
myDate.getTime(); //获取当前时间(从1970.1.1开始的毫秒数)
myDate.getHours(); //获取当前小时数(0-23)
myDate.getMinutes(); //获取当前分钟数(0-59)
myDate.getSeconds(); //获取当前秒数(0-59)
myDate.getMilliseconds(); //获取当前毫秒数(0-999)
myDate.toLocaleDateString(); //获取当前日期
var mytime=myDate.toLocaleTimeString(); //获取当前时间
myDate.toLocaleString( ); //获取日期与时间
if (mytime<"23:30:00")
{
alert(mytime);
}




<script language="javascript">
//日期加减
function addDate(date,days){
var d=new Date(date);
d.setDate(d.getDate()+days);
var m=d.getMonth()+1;
return d.getFullYear()+'-'+m+'-'+d.getDate();
}

var dd="2014-02-28".split("-");
var today = new Date(dd[0],dd[1]-1,dd[2]);
document.write(addDate(today,1));



<script language="javascript">
//日期加减
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;
}

Date.prototype.addDays = function(d){
this.setDate(this.getDate() + d);
};

Date.prototype.addWeeks = function(w){
this.addDays(w * 7);
};

Date.prototype.addMonths= function(m){
var d = this.getDate();
this.setMonth(this.getMonth() + m);

if (this.getDate() < d)
this.setDate(0);
};


Date.prototype.addYears = function(y){
var m = this.getMonth();
this.setFullYear(this.getFullYear() + y);

if (m < this.getMonth())
{
this.setDate(0);
}
};

var dd="2014-4-04".split("-");
var today = new Date(dd[0],dd[1]-1,dd[2]);

today.addDays(27);//加减日期操作
//document.write(today.getFullYear()+"-"+today.getMonth()+"-"+today.getDate());
document.write(today.Format("yyyy-MM-dd"));



[color=red]以上代码非原创[/color] 阅读更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: