When using amp-date-picker
and use it to save an amp-state
both event.start
and event.end
return 1 day after the date selected.
Example: I pick 5/15/2019 - 5/22/2019, it returns 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"
After much searching I was unable to find why my dates were returning incorrectly, and finding that AMP has no date manipulation functionality. What I had to do was hack the system by creating a date editor script and calling it using amp-state
src.
AMP Code:
<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 Script:
<?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);
Hopefully this helps someone in the future, and if someone finds a better solution I would live the hear about it.