amp-html - amp-date-picker 返回不正确的日期
问题描述
使用amp-date-picker
并使用它来保存amp-state
两者event.start
并event.end
在所选日期后 1 天返回。
示例:我选择 5/15/2019 - 5/22/2019,它返回 5/16/2019 - 5/23/2019。
<!doctype html>
<html amp lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1">
<link rel="canonical" href="test.html" hreflang="en-us">
<script async src="https://cdn.ampproject.org/v0.js"></script>
<script async custom-template="amp-mustache" src="https://cdn.ampproject.org/v0/amp-mustache-0.2.js"></script>
<script async custom-element="amp-bind" src="https://cdn.ampproject.org/v0/amp-bind-0.1.js"></script>
<script async custom-element="amp-date-picker" src="https://cdn.ampproject.org/v0/amp-date-picker-0.1.js"></script>
<style amp-boilerplate>body{-webkit-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation:-amp-start 8s steps(1,end) 0s 1 normal both;animation:-amp-start 8s steps(1,end) 0s 1 normal both}@-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}</style><noscript><style amp-boilerplate>body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}</style></noscript>
</head>
<body>
<amp-state id="filterState">
<script type="application/json">
{
"dates": -1,
"startDate": -1,
"endDate": -1
}
</script>
</amp-state>
<amp-date-picker type="range" mode="static" height="360" format="MM/DD/YYYY" min="" on="select:AMP.setState({ filterState: { dates: event.dates, startDate: event.start, endDate: event.end } })"></amp-date-picker>
</body>
</html>
AMP.printState()
filterState:
dates: Array(8)
0: {date: "05/16/2019", id: null}
1: {date: "05/17/2019", id: null}
2: {date: "05/18/2019", id: null}
3: {date: "05/19/2019", id: null}
4: {date: "05/20/2019", id: null}
5: {date: "05/21/2019", id: null}
6: {date: "05/22/2019", id: null}
7: {date: "05/23/2019", id: null}
length: 8
endDate: "05/23/2019"
startDate: "05/16/2019"
解决方案
经过大量搜索,我无法找到为什么我的日期返回不正确,并且发现 AMP 没有日期操作功能。我必须做的是通过创建一个日期编辑器脚本并使用amp-state
src 调用它来破解系统。
安培代码:
<amp-state id="filterDates" src="https://www.test.com/date.php" [src]="(filterState.startDate != -1 || filterState.endDate != -1)?'https://www.test.com/date.php?start='+encodeURIComponent(filterState.startDate)+'&end='+encodeURIComponent(filterState.endDate):'https://www.test.com/date.php'"></amp-state>
PHP 脚本:
<?php
header('Content-type: application/json');
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Allow-Origin: *');
header('AMP-Access-Control-Allow-Source-Origin: https://www.test.com');
header('Content-Type: application/json');
$return = array(
'startDate' => -1,
'endDate' => -1
);
if(isset($_GET['start']))
{
$return['startDate'] = date("m/d/Y", strtotime("-1 day", strtotime($_GET['start'])));
}
if(isset($_GET['end']))
{
$return['endDate'] = date("m/d/Y", strtotime("-1 day", strtotime($_GET['end'])));
}
echo json_encode($return);
希望这对将来的某人有所帮助,如果有人找到更好的解决方案,我会听到它。
推荐阅读
- mpdf - mPDF CSS 类,用于嵌套的文本颜色
- 被父母的颜色覆盖
- java - itext 将 PDF 拆分为多个 PDF,但大小相同
- json - 是否可以将 JSON 子对象直接读取为 JSON DOM 对象或 Data 或其他一些通用格式?
- python - Cloudera Data Science Workbench 访问 Windows Server 上的共享驱动器
- swiftui - 如何在swiftUI中的List中设置空列背景颜色?
- r - 如何将字符串转换为变量并循环通过 group_by?
- python - 将用户输入与文本文件进行比较
- ruby - 如何在 Sinatra 中使用 DSL 元编程
- python - 在 Pandas DataFrame 中附加字符串并累积计数
- android - java.lang.IllegalStateException: getCallbackCaptor.capture() 不能为空