js 求时间变量 上月 昨天 6个月前

easyman123 2018-01-04 10:13:50
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;
}


var date=new Date;
var year=date.getFullYear();
var month=date.getMonth()+1;
month =(month<10 ? "0"+month:month);
var half_month = (year.toString()+month.toString()-7);//6个月前
var month = (year.toString()+month.toString()-1);//上月
var yday=new Date(new Date().getTime()-1*24*60*60*1000).Format("yyyy-MM-dd");//昨天
var tday=new Date(new Date().getTime()).Format("yyyy-MM-dd");//今天


这是我的kettle里的js写的,但为什么翻年了 就变成201800了,6个月期前也不对,求大神
...全文
600 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
roffer 2018-01-04
  • 打赏
  • 举报
回复

window.console = window.console || {log:function(s){alert(s);}};

	Date.prototype.calcAndFormat = function(num,type,format){
		var m = this.getMonth();
		var d = this.getDate();

		if(type == 1){//天
			this.setDate(d + num);
		}else if(type == 2){//月
			this.setMonth(m + num)
		}

		var o = {  
	        "M+": this.getMonth()+1 , //月份   
	        "d+": this.getDate(), //日   
	        "h+": this.getHours(), //小时   
	        "m+": this.getMinutes(), //分   
	        "s+": this.getSeconds(), //秒     
	        "S": this.getMilliseconds() //毫秒   
	    };  
	    if (/(y+)/.test(format)) 
	    	format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));  
	    for (var k in o)  
	    	if (new RegExp("(" + k + ")").test(format)) 
	    		format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ('0'+o[k]).slice(-2));  
	    
	    return format;   
	}

	var D = new Date();
	var format = D.calcAndFormat(-1,1,'yyyy-MM-dd');
	console.log(format);
文盲老顾 2018-01-04
  • 打赏
  • 举报
回复
其实js对日期的计算非常有意思,弱类型到不能再弱类型了,比如,声明2018年1月1日,可以使用 new Date(2018,0,1),如果声明2017年12月31日,则可以new Date(2017,11,31),也可以new Date(2018,0,0),还可以new Date(2018,-1,31),也就是说js会自动将你声明的时间转换成合法时间 那么接下来就简单了

var d = new Date(); // 获取当前时间
var yesterday = new Date(d.getFullYear(),d.getMonth(),d.getDate()-1); // 日期位减1,表示前一天
var half_years_ago = new Date(d.getFullYear(),d.getMonth()-6,d.getDate()); // 月份位减6,表示六个月前
Mike_csy 2018-01-04
  • 打赏
  • 举报
回复
直接在开发工具上是不认识console的(我用的eclipse)。你在网页上试一下。而且console.log()只是一个输出语句,可以不要,或者换为alert()试一下。(这回复验证码我是真的服了)
easyman123 2018-01-04
  • 打赏
  • 举报
回复
ReferenceError: "console" is not defined. (script#33) 为什么用你的在kettle里要报错
Mike_csy 2018-01-04
  • 打赏
  • 举报
回复
你那些日期基本上都是字符串和数字之间的运算当然会出错;我试了一下这样应该可以: var date=new Date(); date.setMonth(date.getMonth()-6);//6个月前 console.log(date); var Month = date.getMonth(); Month = (Month+1<10)?"0"+Month:Month; var year = date.getFullYear(); var half_Month = year+""+Month; console.log(half_Month);
clark_kidd 2018-01-04
  • 打赏
  • 举报
回复
Date.prototype.addDays = function (d) {
    return new Date(this.valueOf() + d * 24 * 60 * 60 * 1000);
}
alert(new Date().addDays(7));
alert(new Date().addDays(-1));
按这个思路你自己扩展一下

87,996

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧