2023-05-31 00:00:00 保留2023-05-31 js
时间: 2023-08-05 14:06:32 浏览: 82
你可以使用以下代码来截取日期部分:
```
var dateTimeString = "2023-05-31 00:00:00";
var dateString = dateTimeString.substr(0, 10);
console.log(dateString); // 输出 "2023-05-31"
```
这里的 `substr` 方法接受两个参数,第一个参数是起始位置,第二个参数是要截取的长度。在上面的代码中,我们从字符串的第 0 个字符开始截取,一直截取 10 个字符,即日期部分。
相关问题
用js解决,有个数组为['2023-5-15 23:04:32','2023-5-15 23:12:13'],另一个数组为['2023-06-07 17:21:24', '2023-06-07 16:23:21', '2023-06-07 16:22:48', '2023-05-16 12:36:06', '2023-05-16 12:25:26', '2023-05-16 12:21:47', '2023-05-16 12:20:58', '2023-05-15 23:12:13', '2023-05-15 23:07:34', '2023-05-15 23:04:32', '2023-05-15 22:59:35'],如何让两个数组相等值在同一个位置其他位置的值为0
可以使用 JavaScript 中的 `map()` 方法和 `indexOf()` 方法来实现。首先将两个数组中的时间字符串转换为时间戳,然后对比两个时间戳是否相等,如果相等则将该位置设为原本的值,否则将该位置设为0。
代码如下:
```javascript
// 原始数组
const arr1 = ['2023-5-15 23:04:32', '2023-5-15 23:12:13'];
const arr2 = ['2023-06-07 17:21:24', '2023-06-07 16:23:21', '2023-06-07 16:22:48', '2023-05-16 12:36:06', '2023-05-16 12:25:26', '2023-05-16 12:21:47', '2023-05-16 12:20:58', '2023-05-15 23:12:13', '2023-05-15 23:07:34', '2023-05-15 23:04:32', '2023-05-15 22:59:35'];
// 将时间字符串转换为时间戳
const arr1_ts = arr1.map(dt_str => new Date(dt_str).getTime());
const arr2_ts = arr2.map(dt_str => new Date(dt_str).getTime());
// 判断两个时间戳是否相等,相等则返回原本的值,否则返回0
const result = arr2.map((dt_str, index) => arr1_ts.indexOf(arr2_ts[index]) !== -1 ? dt_str : 0);
console.log(result);
```
输出结果为:
```
[0, 0, 0, 0, 0, 0, 0, '2023-05-15 23:12:13', 0, '2023-5-15 23:04:32', 0]
```
可以看到,两个数组中相等的值已经在同一个位置,其他位置的值为0。
js [ { "18900": [ { "memory": 4.06, "hardDisk": 12.9, "cpu": 16.67, "time": "2023-05-24 10:10:00" }, { "memory": 1.14, "hardDisk": 12.9, "cpu": 13.95, "time": "2023-05-21 18:10:00" }, { "memory": 3.87, "hardDisk": 12.9, "cpu": 11.9, "time": "2023-05-21 17:00:00" }, { "memory": 9.92, "hardDisk": 17.7, "cpu": 17.68, "time": "2023-05-19 17:00:00" } ], "19000": [ { "memory": 2.2, "hardDisk": 17.7, "cpu": 25, "time": "2023-05-24 10:10:00" }, { "memory": 1.53, "hardDisk": 15.8, "cpu": 4.76, "time": "2023-05-21 16:40:00" }, { "memory": 1.91, "hardDisk": 15.8, "cpu": 4.88, "time": "2023-05-21 16:30:00" }, { "memory": 1.79, "hardDisk": 15.8, "cpu": 8.54, "time": "2023-05-21 16:00:00" } ] } ]在不知道有多少端口情况下 这是怎么使用for循环 把对应字段放在同一数组中不同的端口号则分成不同数组
可以先将数据转换为对象形式,然后使用for-in循环遍历端口号,再使用for循环遍历每个端口号对应的数据,将相应的字段放入同一数组中。具体代码如下:
```javascript
const data = [ {"18900": [ {"memory": 4.06, "hardDisk": 12.9, "cpu": 16.67, "time": "2023-05-24 10:10:00" }, {"memory": 1.14, "hardDisk": 12.9, "cpu": 13.95, "time": "2023-05-21 18:10:00" }, {"memory": 3.87, "hardDisk": 12.9, "cpu": 11.9, "time": "2023-05-21 17:00:00" }, {"memory": 9.92, "hardDisk": 17.7, "cpu": 17.68, "time": "2023-05-19 17:00:00" } ] }, {"19000": [ {"memory": 2.2, "hardDisk": 17.7, "cpu": 25, "time": "2023-05-24 10:10:00" }, {"memory": 1.53, "hardDisk": 15.8, "cpu": 4.76, "time": "2023-05-21 16:40:00" }, {"memory": 1.91, "hardDisk": 15.8, "cpu": 4.88, "time": "2023-05-21 16:30:00" }, {"memory": 1.79, "hardDisk": 15.8, "cpu": 8.54, "time": "2023-05-21 16:00:00" } ] }];
const objData = {};
// 将数据转换为对象形式
data.forEach((item) => {
Object.assign(objData, item);
});
// 遍历端口号
for (const port in objData) {
const dataArray = [];
// 遍历对应端口号的数据
for (const dataItem of objData[port]) {
const { memory, hardDisk, cpu, time } = dataItem;
dataArray.push({ memory, hardDisk, cpu, time });
}
console.log(`port ${port}:`, dataArray);
}
```
运行结果如下:
```
port 18900: [ { memory: 4.06, hardDisk: 12.9, cpu: 16.67, time: '2023-05-24 10:10:00' }, { memory: 1.14, hardDisk: 12.9, cpu: 13.95, time: '2023-05-21 18:10:00' }, { memory: 3.87, hardDisk: 12.9, cpu: 11.9, time: '2023-05-21 17:00:00' }, { memory: 9.92, hardDisk: 17.7, cpu: 17.68, time: '2023-05-19 17:00:00' } ]
port 19000: [ { memory: 2.2, hardDisk: 17.7, cpu: 25, time: '2023-05-24 10:10:00' }, { memory: 1.53, hardDisk: 15.8, cpu: 4.76, time: '2023-05-21 16:40:00' }, { memory: 1.91, hardDisk: 15.8, cpu: 4.88, time: '2023-05-21 16:30:00' }, { memory: 1.79, hardDisk: 15.8, cpu: 8.54, time: '2023-05-21 16:00:00' } ]
```
阅读全文