Javascript教程
JavaScript基础
JavaScript Objects
JavaScript BOM
JavaScript DOM
JavaScript OOP
JavaScript Cookies
JavaScript事件
JavaScript异常
JavaScript常用

Javascript 日期比较

在上一节中,我们讨论了日期方法以及构造函数。
在这里,借助这些方法,我们将学习比较日期。
基本上,可以通过多种方法比较日期,例如:
比较两个日期。 比较日期和时间。 使用getTime()比较日期

将两个日期相互比较

示例:
<html>
<head> Comparing Dates</br></head>
<body>
<script>
function compare()
{
var d1=new Date('2020-01-23'); //yyyy-mm-dd
var d2=new Date('2020-01-21'); //yyyy-mm-dd
if(d1>d2)
{
document.write("True, First date is greater than second date");
}
else if(d1<d2)
{
document.write("False, Second date is smaller than the first");
}
else
{
document.write("Both date are same and equal");
}
}
compare(); //invoking compare()
</script>
</body>
</html>

比较日期与时间

示例1:比较具有不同时间的不同日期
<html>
<head> Comparing Date and time</br></head>
<body>
<script>
var d1=new Date("Apr 17, 2019 12:10:10"); //mm dd, yyyy hh:mm:ss
var d2=new Date("Dec 1, 2019 12:10:30"); //mm dd, yyyy hh:mm:ss
if(d1>d2)
{
document.write("False, d1 date and time is smaller than d2 date and time");
}
else if(d1<d2)
{
document.write("True, d2 is greater in terms of both time and date");
}
else
{
document.write("Both date and time are same and equal");
}
</script>
</body>
</html>
示例2:比较具有相同日期不同时间
<html>
<head> Comparing same date but different time</br></head>
<body>
<script>
var d1=new Date("2018-01-10, 12:10:10"); //yyyy-mm-dd hh:mm:ss
var d2=new Date("2018-01-10, 12:10:50"); //yyyy-mm-dd hh:mm:ss
if(d1>d2)
{
document.write("False, d1 & d2 dates are same but d2 time is greater than d1 time");
}
else if(d1<d2)
{
document.write("True, d2 time is greater than d1 time.");
}
else
{
document.write("Both date and time are same and equal");
}
</script>
</body>
</html>

当前时间和getTime()比较

在日期之间进行比较的一种更好的方法是使用 getTime()函数。此功能可将日期转换为数值以直接比较它们。
示例1 :将当前日期和时间与给定的日期和时间进行比较。
<html>
<head> Comparing Dates</br></head>
<body>
<script>
var d1=new Date("2019-10-10, 10:10:10"); //yyyy-mm-dd hh:mm:ss
var currentdate=new Date(); //fetch the current date value
if(d1.getTime()<currentdate.getTime())
{
document.write("True, currentdate and time are greater than d1");
}
else if(d1.getTime()>currentdate.getTime())
{
document.write("False");
}
else
{
document.write("True, equal");
}
</script>
</body>
</html>
示例2:比较两个具有不同时间的不同日期。
<html>
<head> Comparing Dates</br></head>
<body>
<script>
var d1=new Date("2019-10-10, 10:10:10");
var d2=new Date("2019-11-02, 14:19:05");
if(d1.getTime()<d2.getTime())
{
document.write("True, d1 date and time are smaller than d2 date and time");
}
else if(d1.getTime()>d2.getTime())
{
document.write("False, d2 date and time are greater than d1");
}
else
{
document.write("True, d1 and d2 have same time and date");
}
</script>
</body>
</html>
因此,我们可以用多种可能的方式比较日期。

更改日期格式

我们还可以通过JavaScript代码更改或设置格式。函数getFullYear(),GetMonth()和getDate()允许相应地设置日期格式。
示例1:将日期格式更改为'yyyy-mm-dd'。
<html>
<head> <h3>Changing date format</h3></br></head>
<body>
<script>
var current_date=new Date(); //fetches current date
var set_to=current_date.getFullYear()+"-"+(current_date.getMonth()+1)+"-"+current_date.getDate();
document.write("The format followed is yyyy-dd-mm: "+set_to);
</script>
</body>
</html>
我们还可以根据需要设置日期和时间格式。
示例2:将日期时间格式更改为'yyyy-dd-mm hh:mm:ss'。
<html>
<head> <h3>Changing date format</h3></br></head>
<body>
<script>
var current_datetime=new Date(); //fetches current date and time
var set_to=current_datetime.getFullYear()+"-"+(current_datetime.getMonth()+1)+"-"+current_datetime.getDate()+" "+current_datetime.getHours()+":"+current_datetime.getMinutes()+":"+current_datetime.getSeconds();
document.write("The format followed is yyyy-dd-mm hh:mm:ss : "+set_to);
</script>
</body>
</html>
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4