所以,我正在研究一个时间注册管理工具。我有一个日期选择器组件,我可以在其中选择要查看注册的日期。它给了我当天的Unix时间戳,我存储了这个值,fx.1606856503。
现在,我正在从 API 检索所有注册,API 是一个对象数组,其中每个对象都是一个注册。每个注册都有一个日期属性,它基本上是从创建之日起的 Unix 时间戳。
[{
"id": "1",
"userId": "userId 1",
"customerId": "customerId 1",
"case": "case 1",
"description": "description 1",
"hours": 72,
"date": 1606826246,
"customer": "customer 1",
"project": "project 1"
}]
现在,我有一个日期选择器Unix时间戳,我想过滤注册,以便仅过滤和显示从选择器获得Unix时间戳当天进行的注册,但无法弄清楚我将如何比较它们并根据日期进行过滤。
下面是一个快速代码片段,说明了如何使用Date.prototype.toISOString()
或Date.prototype.toDateString()
根据指定的时间戳进行筛选。
由于时间戳以秒为单位存储,而javascript日期使用毫秒,因此在创建日期时需要乘以1000
const filterTimestamp = 1606859476; // Tuesday, December 1, 2020 9:51:16 PM
const filterDate = new Date(filterTimestamp*1000);
然后,您可以通过比较toISOString()
返回的日期字符串的前 10 个字符进行筛选,这将始终将时区保留为零 UTC 偏移
const filterDateString = new Date(filterTimestamp*1000).toISOString().slice(0, 10);
// "2020-12-01" sliced from "2020-12-01T21:51:16.000Z"
const regsOnDate = regs.filter(o => (
new Date(o.date*1000).toISOString().slice(0, 10) === filterDateString));
或按将使用本地时区的toDateString()
返回的日期字符串
const filterDateString = new Date(filterTimestamp*1000).toDateString();
const regsOnDate = regs.filter(o => (
new Date(o.date*1000).toDateString() === filterDateString));
// eg: compares "Mon Nov 02 2020" to "Tue Dec 01 2020"
<小时 />使用toISOString()
const regs = [
{
"id": "1",
"date": 1606826246, // Tuesday, December 1, 2020 12:37:26 PM
"customer": "customer 1",
},
{
"id": "2",
"date": 1604353553, // Monday, November 2, 2020 9:45:53 PM
"customer": "customer 2",
},
{
"id": "3",
"date": 1606860022, // Tuesday, December 1, 2020 10:00:22 PM
"customer": "customer 3",
}
]
const filterTimestamp = 1606859476; // Tuesday, December 1, 2020 9:51:16 PM
const filterDateString = new Date(filterTimestamp*1000).toISOString().slice(0, 10);
// "2020-12-01" sliced from "2020-12-01T21:51:16.000Z"
const regsOnDate = regs.filter(o => (
new Date(o.date*1000).toISOString().slice(0, 10) === filterDateString));
console.log( regsOnDate );
<小时 />使用toDateString()
const regs = [
{
"id": "1",
"date": 1606826246, // Tuesday, December 1, 2020 12:37:26 PM
"customer": "customer 1",
},
{
"id": "2",
"date": 1604353553, // Monday, November 2, 2020 9:45:53 PM
"customer": "customer 2",
},
{
"id": "3",
"date": 1606860022, // Tuesday, December 1, 2020 10:00:22 PM
"customer": "customer 3",
}
]
const filterTimestamp = 1606859476; // Tuesday, December 1, 2020 9:51:16 PM
const filterDateString = new Date(filterTimestamp*1000).toDateString();
const regsOnDate = regs.filter(o => (
new Date(o.date*1000).toDateString() === filterDateString));
console.log( regsOnDate );
UNIX 时间戳是自 1970 年 1 月 1 日 UTC 以来的秒,ECMAScript 时间值是自同一纪元以来的毫秒,请参阅JavaScript 中的将 Unix 时间戳转换为时间。
如果您只想比较同一日期 UTC 的值,则可以只比较自纪元以来的整天。这意味着对值进行简单的算术运算,而不是使用 Date 对象。例如
let data = [{"id": "1","date": 1606826246}, // 1 Dec 2020 UTC
{"id": "2","date": 1606867200}, // 2 Dec 2020 UTC
{"id": "3","date": 1606953600} // 3 Dec 2020 UTC
];
// Start with date sometime on 2 Dec 2020 UTC
let d = new Date(Date.UTC(2020,11,2,8,32,21)); // 2 Dec 2020 08:32:21 Z
console.log('Test date: ' + d.toISOString());
// Get whole days since epoch
let daysSinceEpoch = d.getTime() / 8.64e7 | 0;
console.log('daysSinceEpoch: ' + daysSinceEpoch);
// Find matching records in data
let result = data.filter(obj => (obj.date / 8.64e4 | 0) == daysSinceEpoch);
console.log('Matching records: ' + JSON.stringify(result));
// Matching date values
console.log('Matching dates:');
result.forEach(obj => console.log('id: ' + obj.id + ' on ' + new Date(obj.date * 1000).toISOString().substr(0,10)));
你可以对本地日期做同样的事情,你只需要更加小心地获取天数,请参阅如何在 JavaScript 中获取两个日期之间的天数?