有人能提出一种方法来比较两个大于、小于和过去不使用JavaScript的日期的值吗?值将来自文本框。
当前回答
注意-仅比较日期部分:
当我们在javascript中比较两个日期时。这需要数小时、数分钟和数秒的时间。。因此,如果我们只需要比较日期,这是一种方法:
var date1= new Date("01/01/2014").setHours(0,0,0,0);
var date2= new Date("01/01/2014").setHours(0,0,0,0);
现在:如果date1.valueOf()>date2.valueOf。
其他回答
减去两个日期,得到以毫秒为单位的差值,如果得到0,则是相同的日期
function areSameDate(d1, d2){
return d1 - d2 === 0
}
Date对象将执行您想要的操作-为每个日期构造一个,然后使用>、<、<=或>=对它们进行比较。
==,!=,==,和!==运算符要求您使用date.getTime(),如中所示
var d1 = new Date();
var d2 = new Date(d1);
var same = d1.getTime() === d2.getTime();
var notSame = d1.getTime() !== d2.getTime();
明确地说,直接检查日期对象是否相等是行不通的
var d1 = new Date();
var d2 = new Date(d1);
console.log(d1 == d2); // prints false (wrong!)
console.log(d1 === d2); // prints false (wrong!)
console.log(d1 != d2); // prints true (wrong!)
console.log(d1 !== d2); // prints true (wrong!)
console.log(d1.getTime() === d2.getTime()); // prints true (correct)
不过,我建议您使用下拉列表或类似的受约束的日期输入形式,而不是文本框,以免您陷入输入验证地狱。
对于好奇的date.getTime()文档:
返回指定日期的数值,即自1970年1月1日00:00:00 UTC以来的毫秒数。(之前的时间返回负值。)
我通常将日期作为时间戳(数字)存储在数据库中。
当我需要比较时,我只需在这些时间戳或
将其转换为Date Object,然后根据需要与><进行比较。
请注意,==或==无法正常工作,除非变量是同一日期对象的引用。
首先将这些Date对象转换为时间戳(数字),然后比较它们的相等性。
日期到时间戳
var timestamp_1970 = new Date(0).getTime(); // 1970-01-01 00:00:00
var timestamp = new Date().getTime(); // Current Timestamp
截至日期的时间戳
var timestamp = 0; // 1970-01-01 00:00:00
var DateObject = new Date(timestamp);
function compare_date(date1, date2){
const x = new Date(date1)
const y = new Date(date2)
function checkyear(x, y){
if(x.getFullYear()>y.getFullYear()){
return "Date1 > Date2"
}
else if(x.getFullYear()<y.getFullYear()){
return "Date2 > Date1"
}
else{
return checkmonth(x, y)
}
}
function checkmonth(x, y){
if(x.getMonth()>y.getFullYear()){
return "Date1 > Date2"
}
else if(x.getMonth()<y.getMonth){
return "Date2 > Date1"
}
else {
return checkDate(x, y)
}
}
function checkDate(x, y){
if(x.getDate()>y.getFullYear()){
return "Date1 > Date2"
}
else if(x.getDate()<y.getDate()){
return "Date2 > Date1"
}
else {
return checkhour(x,y)
}
}
function checkhour(x, y){
if(x.getHours()>y.getHours()){
return "Date1 > Date2"
}
else if(x.getHours()<y.getHours()){
return "Date2 > Date1"
}
else {
return checkhmin(x,y)
}
}
function checkhmin(x,y){
if(x.getMinutes()>y.getMinutes()){
return "Date1 > Date2"
}
else if(x.getMinutes()<y.getMinutes()){
return "Date2 > Date1"
}
else {
return "Date1 = Date2"
}
}
return checkyear(x, y)
仅比较日期(忽略时间分量):
Date.prototype.sameDay = function(d) {
return this.getFullYear() === d.getFullYear()
&& this.getDate() === d.getDate()
&& this.getMonth() === d.getMonth();
}
用法:
if(date1.sameDay(date2)) {
// highlight day on calendar or something else clever
}
我不再建议修改内置对象的原型。请尝试以下操作:
函数为SameDay(d1,d2){return d1.getFullYear()==d2.getFullYear()&&d1.getDate()==d2.getDate&&d1.getMonth()==d2.getMont();}console.log(isSameDay(新日期(‘2021 1月15日02:39:53 GMT-0800’),新日期(’2021 1月15号23:39:53格林威治时间-0800’));console.log(isSameDay(新日期(2021 1月15日10:39:53 GMT-0800),新日期(2021 1月16日10:39:33 GMT-0800'));
注意:将返回您所在时区的年/月/日;如果您想检查两个日期是否在不同时区的同一天,我建议使用时区感知库。
e.g.
> (new Date('Jan 15 2021 01:39:53 Z')).getDate() // Jan 15 in UTC
14 // Returns "14" because I'm in GMT-08
推荐文章
- 窗口。亲近与自我。close不关闭Chrome中的窗口
- 同步和异步编程(在node.js中)的区别是什么?
- 在d3.js中调整窗口大小时调整svg的大小
- 如何将两个字符串相加,就好像它们是数字一样?
- 绑定多个事件到一个监听器(没有JQuery)?
- 在JavaScript中将JSON字符串解析为特定对象原型
- 将字符串“true”/“false”转换为布尔值
- 单元测试:日期时间。现在
- 如何使用JavaScript代码获得浏览器宽度?
- event.preventDefault()函数在IE中无法工作
- indexOf()和search()的区别是什么?
- 错误:'types'只能在.ts文件中使用- Visual Studio Code使用@ts-check
- React-Native:应用程序未注册错误
- LoDash:从对象属性数组中获取值数组
- src和dist文件夹的作用是什么?