[js]关于Date()的日期转换问题以及JS (intermediate value).Format is not a function问题解决

本文探讨了JavaScript中日期处理的常见问题,包括默认格式理解误区、format方法缺失及替代方案,介绍了如何正确处理日-月-年格式的日期字符串,并提供了使用原始方法拼接日期的解决方案。

首先默认下new Date(),默认内部是参数是月-日-年

因此,当获得控件的值,例如e.target.value,获得可能是日-月-年的格式

因此需要使用format转换,但是最新版本js已经没有了format方法,会报错

JS (intermediate value).Format is not a function问题解决

因此需要到这里: https://github.com/jacwright/date.format

下载并引入使用

注意官方的参数格式,要不打印出来会很奇怪

 

同时需要注意的是,format出来的值其实是string,不能直接getTime(),因此还需要再转换一次

var purchaseTrans = new Date(e.target.value).format("d-m-Y");
var purchaseDate = new Date(purchaseTrans);

***注意: 这个方法也不太可行,因为new Date()里面其实还是先接受了月-日-年的格式,所以当给出13-06-2020这样的时候,它会报错,认为是月份是13

后来换了moment.js来试,感觉也不太行,还是用原始的方法来拼接走:

var form_date_value = app.$data.form.data.purchaseDate.split("-")
var targetDate = new Date(form_date_value[2], form_date_value[1] - 1, form_date_value[0])

 

 

“// 1. 导入点数据和ROI区域 var samplePoints = ee.FeatureCollection("projects/ee-yixuwei610/assets/2024HBsamples"); var roi = ee.FeatureCollection('projects/ee-yixuwei610/assets/hubei'); var roiGeometry = roi.geometry(); // 2. 加载Landsat 8地表反射率数据 var dataset = ee.ImageCollection('LANDSAT/LC08/C02/T1_L2') .filterDate('2023-01-01', '2024-12-01') .filterBounds(roiGeometry); // 3. 缩放因子校正函数 function applyScaleFactors(image) { var opticalBands = image.select('SR_B.').multiply(0.0000275).add(-0.2); return image.addBands(opticalBands, null, true); } dataset = dataset.map(applyScaleFactors); // 4. 计算NDVI函数 function addNDVI(image) { var ndvi = image.normalizedDifference(['SR_B5', 'SR_B4']).rename('NDVI'); return image.addBands(ndvi); } var withNDVI = dataset.map(addNDVI).select('NDVI'); // 5. 创建月度合成影像集合(关键修改) var monthlyCollection = ee.ImageCollection( withNDVI.map(function(image) { return image.set({ 'year': image.date().get('year'), 'month': image.date().get('month'), 'date': image.date().format('YYYY-MM') }); }) .filterDate('2023-01-01', '2024-12-31') ); // 6. 月度中值合成(引用[1][3]方法) var monthlyMedian = monthlyCollection.reduce(ee.Reducer.median().forEachBand(monthlyCollection.first().bandNames())) .rename('NDVI'); // 确保重命名为NDVI // 7. 点采样(高效方法) var sampledPoints = monthlyMedian.sampleRegions({ collection: samplePoints, properties: ['name', 'landcover'], // 保留原始属性 scale: 30, tileScale: 8, geometries: true }); // 8. 添加时间属性 var datedFeatures = sampledPoints.map(function(feature) { var year = feature.get('year'); var month = ee.Number(feature.get('month')).format('%02d'); return feature.set('date', year.cat('-').cat(month)); }); // 9. 导出月度NDVI数据 Export.table.toDrive({ collection: datedFeatures, description: 'Monthly_NDVI_Export', fileFormat: 'CSV', selectors: ['name', 'landcover', 'date', 'NDVI'] }); // 10. 时间序列可视化 var monthlyChart = ui.Chart.image.series({ imageCollection: monthlyMedian, region: samplePoints, reducer: ee.Reducer.mean(), scale: 30 }).setOptions({ title: "月度NDVI时间序列", hAxis: {title: "日期"}, vAxis: {title: "NDVI"}, lineWidth: 2 }); print("月度NDVI时间序列图", monthlyChart); // 11. 地图显示 Map.addLayer(roiGeometry, {color: 'FF0000'}, '湖北'); Map.addLayer(samplePoints, {color: '0000FF'}, '农田点'); Map.centerObject(roiGeometry, 8); ”报错“Line 56: (intermediate value).cat is not a function
最新发布
09-16
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值