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,06,06,06,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() 返回从 197011 日(指定日期)至今的毫秒数
var d=new Date();
document.write("从 1970/01/01 至今已过去 " + d.getTime() + " 毫秒");


posted @   南风晚来晚相识  阅读(7562)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
点击右上角即可分享
微信分享提示