【问题标题】:Fetch today date and add 7 days (for Google Opt-In code)获取今天的日期并添加 7 天(用于 Google 选择加入代码)
【发布时间】:2019-02-13 10:45:38
【问题描述】:

我不知道 Javascript 或 PHP,我已尽力在网上找到答案,但没有基本知识,我不知道如何做到这一点。我相信有很多人和我一样被卡住了。

我的情况是我需要通过某种方式获取当前日期并添加7天来插入交货日期,然后将其输入到下面代码中的“YYYY-MM-DD”字段中。

我找到的最佳答案是here,但我不知道如何实现它

这是完整的代码,请帮我弄清楚如何构造它:

<!-- BEGIN GCR Opt-in Module Code -->
<script src="https://apis.google.com/js/platform.js?onload=renderOptIn"
  async defer>
</script>

<script>
  window.renderOptIn = function() { 
    window.gapi.load('surveyoptin', function() {
      window.gapi.surveyoptin.render(
        {
          // REQUIRED
          "merchant_id":"xxxxxxxxxx",
          "order_id": "{BOOKINGNUMBER}",
          "email": "{EMAIL}",
          "delivery_country": "ISO 3166-2:CA",
          "estimated_delivery_date": "YYYY-MM-DD",

          // OPTIONAL
          "opt_in_style": "CENTER_DIALOG"
        }); 
     });
  }
</script>
<!-- END GCR Opt-in Module Code -->

【问题讨论】:

标签: javascript php html google-surveys


【解决方案1】:

sevenDaysFromNow 将获取当前日期和时间,然后我将其设置为当前日期和时间 + 7 天。 你可以在这里阅读setDate / getDate https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setDate

然后我使用formatDate 函数将其转换为 yyyy-mm-dd。 转换日期格式取自此处 Format JavaScript Date to yyyy-mm-dd

这是结果:

<!-- BEGIN GCR Opt-in Module Code -->
<script src="https://apis.google.com/js/platform.js?onload=renderOptIn"
  async defer>
</script>
<script>
var sevenDaysFromNow = new Date().setDate(new Date().getDate() + 7);
function formatDate(date) {
    var d = new Date(date),
        month = '' + (d.getMonth() + 1),
        day = '' + d.getDate(),
        year = d.getFullYear();

    if (month.length < 2) month = '0' + month;
    if (day.length < 2) day = '0' + day;

    return [year, month, day].join('-');
}
  window.renderOptIn = function() { 
    window.gapi.load('surveyoptin', function() {
      window.gapi.surveyoptin.render(
        {
          // REQUIRED
          "merchant_id":"xxxxxxxxxx",
          "order_id": "{BOOKINGNUMBER}",
          "email": "{EMAIL}",
          "delivery_country": "ISO 3166-2:CA",
          "estimated_delivery_date": formatDate(sevenDaysFromNow),

          // OPTIONAL
          "opt_in_style": "CENTER_DIALOG"
        }); 
     });
  }
</script>
<!-- END GCR Opt-in Module Code -->

【讨论】:

  • 感谢您的回答,但我收到错误 404 无法加载资源:服务器响应状态为 404 ()
猜你喜欢
  • 2023-04-03
  • 2012-10-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-05
  • 2012-09-22
  • 2016-12-23
  • 1970-01-01
相关资源
最近更新 更多