In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article shows you what kind of pit you step on when comparing JavaScript date objects. The content is concise and easy to understand. It will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
Intuitively, the comparison between two identical dates should be equal, but the result is not like this:
Const D1 = new Date ('2019-06-01'); const D2 = new Date ('2018-06-01'); const D3 = new Date ('2019-06-01'); D1 = = D3; / / false D1 = = D3; / / false
As you can see, whether you use = = or = =, the result is false. It's not surprising to think about it, after all, they are two separate JS objects, not variables of the basic data type. So how do you tell if the dates are equal?
You can use toString () or valueOf (). The toString () method of the date object converts the date to an ISO date string, while the valueOf () method converts the date to a timestamp in millisecond numeric form.
Const D1 = new Date ('2019-06-01'); const D2 = new Date ('2018-06-01'); const D3 = new Date ('2019-06-01'); / / Sat Jun 01 2019 08:00:00 GMT+0800 (China Standard time) d1.toString (); d1.valueOf (); / 1559347200000 d1.toString () = = d2.toString (); / / false d1.toString () = = d3.toString (); / / true d1.valueOf () = = d2.valueOf () / / false d1.valueOf () = = d3.valueOf (); / / true
Interestingly, although = and = = cannot be used to compare date objects
< 和>But you can:
D1
< d2; // false d1 < d3; // false d2 < d1; // true 因此,要判断日期a是否在日期 b之前,只要判断a < b是否为true。另外,日期之间还能用-操作符相减,返回毫秒差值。 const d1 = new Date('2019-06-01'); const d2 = new Date('2018-06-01'); const d3 = new Date('2019-06-01'); d1 - d3; // 0 d1 - d2; // 1 年的毫秒数, 1000 * 60 * 60 * 24 * 365 也就是说,你可以用a - b 结果的正负来判断两个日期的先后。 数组排序的坑 日期对象数组排序的结果很可能出乎意料。比如下面这个排序: const d1 = new Date('2017-06-01'); const d2 = new Date('2018-06-01'); const d3 = new Date('2019-06-01'); [d2, d1, d3].sort(); // [d2, d3, d1] 按理说从小到大排序应该是[d1, d2, d3],结果很意外。这是为什么呢?原来,JavaScript 数组的sort方法默认是比较元素的字符串形式。因此,上面的sort实际上是基于下面的结果来排序的: [ 'Fri Jun 01 2018 08:00:00 GMT+0800 (中国标准时间)', 'Sat Jun 01 2019 08:00:00 GMT+0800 (中国标准时间)', 'Thu Jun 01 2017 08:00:00 GMT+0800 (中国标准时间)' ] 怎么解决这个问题?很简单,传一个自定义的排序函数compare()给sort()方法。这个compare()函数的返回值确定了两个元素的大小(先后顺序): 0 表示 a 和b 相等 正值表示 a >B, that is, a comes after b
A negative value means a
< b,也就是a在b前面 由于 JavaScript 日期对象可以直接相减,那这个比较函数就很简单了: const d1 = new Date('2017-06-01'); const d2 = new Date('2018-06-01'); const d3 = new Date('2019-06-01'); [d2, d1, d3].sort((a, b) =>A-b); / / [D1, D2, d3]
The next time you encounter the default sort of array array, you won't be surprised by this result:
Const a = [1,4,3,12]; a.sort (); / / [1,12,3,4]
So to prevent Bug, you should pass in a custom sorting function.
The above is what kind of hole you step on when comparing JavaScript date objects. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.