js比较两个时间的大小

在工作中;我们可能会遇见比较两个时间的大小;
这个时候;我们就可以使用比较一下两个时间的大小
需要注意的是了解 
//参考地址:  https://www.cnblogs.com/Byme/p/7687313.html

function tab(date1,date2){
	var oDate1 = new Date(date1);
	var oDate2 = new Date(date2);
	if(oDate1.getTime() > oDate2.getTime()){
		console.log('第一个大');
	} else  if(oDate1.getTime() < oDate2.getTime()){
		console.log('第二个大');
	}else if(oDate1.getTime() == oDate2.getTime()){
			console.log('一样大');
	}
}
tab('2015-10-10 00:00:00','2015-10-03 00:00:00');
tab('2015-10-10','2015-10-03');
这样的时间格式都是可以的;
需要注意的是中间的分割符号 '-'必须是英文的;否者不能够比较;
今天不晓得为啥,写成了中文符号,英文符号中中文符号是有区别的
时间格式这一种,要使用英文符号



new Date()参数格式如下:( 得到一个中国标准时间 )

1、用整数初始化日期对象 
var date1 = new Date(2017,06,06); console.log(date1); // Thu Jul 06 2017 00:00:00 GMT+0800 (中国标准时间) 
var date1 = new Date(2017,1,1); console.log(date1); // Wed Feb 01 2017 00:00:00 GMT+0800 (中国标准时间) 
var date1 = new Date(2017,01-2,01); console.log(date1); // Thu Dec 01 2016 00:00:00 GMT+0800 (中国标准时间) 
var date1 =new Date(2017,06); console.log(date1); // Thu Jul 06 2017 06:06:06 GMT+0800 (中国标准时间) 
说明: new Date( year,month,date,hrs,min,sec) 按给定的参数创建一日期对象

2、用字符串初始化日期对象 
var date2 = new Date(“2017/06/06”); console.log(date2); // Tue Jun 06 2017 00:00:00 GMT+0800 (中国标准时间) 
var date2 = new Date(“2017-08-08”); console.log(date2); // Tue Aug 08 2017 08:00:00 GMT+0800 (中国标准时间) 
var date2 = new Date(“2017-9-9”); console.log(date2); // Sat Sep 09 2017 00:00:00 GMT+0800 (中国标准时间) 




getTime() 返回从 1970 年 1 月 1 日(指定日期)至今的毫秒数
var d=new Date();
document.write("从 1970/01/01 至今已过去 " + d.getTime() + " 毫秒");


相关文章

kindeditor4.x代码高亮功能默认使用的是prettify插件,prett...
这一篇我将介绍如何让kindeditor4.x整合SyntaxHighlighter代...
js如何实现弹出form提交表单?(图文+视频)
js怎么获取复选框选中的值
js如何实现倒计时跳转页面
如何用js控制图片放大缩小