You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何自动替换JSON-LD Event的StartDate和EndDate为当日及次日日期?

实现每日重复活动的动态JSON-LD日期替换

静态HTML无法自动修改日期,需要通过前端JavaScript或后端渲染的方式实现动态替换,以下是两种可行方案:

方案1:前端JavaScript动态生成JSON-LD

在页面加载时通过JS计算当日和次日日期,动态构建并插入JSON-LD脚本:

// 计算+03:00时区的当前日期,避免客户端时区干扰
function getDateInTimezone(offsetHours) {
  const now = new Date();
  const utcTime = now.getTime() + now.getTimezoneOffset() * 60000;
  return new Date(utcTime + offsetHours * 3600000);
}

const targetDate = getDateInTimezone(3);
const currentDateStr = targetDate.toISOString().split('T')[0];

// 计算次日日期
const nextDay = new Date(targetDate);
nextDay.setDate(targetDate.getDate() + 1);
const nextDateStr = nextDay.toISOString().split('T')[0];

// 构建活动Schema数据
const eventSchema = {
  "@context": "https://schema.org",
  "@type": "Event",
  "name": "Name of event",
  "startDate": `${currentDateStr}T15:00+03:00`,
  "endDate": `${nextDateStr}T17:30+03:00`,
  "eventStatus": "https://schema.org/EventMovedOnline",
  "eventAttendanceMode": "https://schema.org/OnlineEventAttendanceMode",
  "location": {
    "@type": "VirtualLocation",
    "url": "https://site"
  },
  // 可选:添加重复频率标记,明确每日重复规则
  "repeatFrequency": "P1D"
};

// 创建并插入脚本标签到页面头部
const script = document.createElement('script');
script.type = 'application/ld+json';
script.textContent = JSON.stringify(eventSchema, null, 2);
document.head.appendChild(script);

方案2:后端渲染生成(更适合爬虫抓取)

如果你的网站由后端语言驱动,直接在后端计算日期并生成JSON-LD,兼容性更好:

PHP示例

<?php
// 设置时区为+03:00,根据实际地域调整
date_default_timezone_set('Europe/Istanbul');
$currentDate = date('Y-m-d');
$nextDay = date('Y-m-d', strtotime('+1 day'));

$eventSchema = [
  "@context" => "https://schema.org",
  "@type" => "Event",
  "name" => "Name of event",
  "startDate" => $currentDate . "T15:00+03:00",
  "endDate" => $nextDay . "T17:30+03:00",
  "eventStatus" => "https://schema.org/EventMovedOnline",
  "eventAttendanceMode" => "https://schema.org/OnlineEventAttendanceMode",
  "location" => [
    "@type" => "VirtualLocation",
    "url" => "https://site"
  ],
  "repeatFrequency" => "P1D"
];
?>

<script type="application/ld+json">
<?php echo json_encode($eventSchema, JSON_PRETTY_PRINT); ?>
</script>

Node.js(Express)示例

app.get('/', (req, res) => {
  const targetOffset = 3 * 60;
  const utcNow = new Date(Date.now() + new Date().getTimezoneOffset() * 60000);
  const targetDate = new Date(utcNow.getTime() + targetOffset * 60000);
  
  const currentDateStr = targetDate.toISOString().split('T')[0];
  const nextDay = new Date(targetDate);
  nextDay.setDate(targetDate.getDate() + 1);
  const nextDateStr = nextDay.toISOString().split('T')[0];

  const eventSchema = {
    "@context": "https://schema.org",
    "@type": "Event",
    "name": "Name of event",
    "startDate": `${currentDateStr}T15:00+03:00`,
    "endDate": `${nextDateStr}T17:30+03:00`,
    "eventStatus": "https://schema.org/EventMovedOnline",
    "eventAttendanceMode": "https://schema.org/OnlineEventAttendanceMode",
    "location": {
      "@type": "VirtualLocation",
      "url": "https://site"
    },
    "repeatFrequency": "P1D"
  };

  res.render('index', { eventSchema: JSON.stringify(eventSchema, null, 2) });
});

模板中插入:

<script type="application/ld+json">
{{ eventSchema }}
</script>

内容的提问来源于stack exchange,提问作者Aleks Last

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 01:50:14