9

I need to the date format that is shown in <lastmod></lastmod> tags in sitemap.xml. How can I achieve that?

Output like:

<lastmod>2016-01-21EEST18:01:18+03:00</lastmod>

Where 'EEST' is probably timezone offset.

I have seen this question and the answers in php:

Google Sitemap Date Format

but don't know how to achieve it in Javascript,

Thanks.

EDIT: the question is not duplicate, since I need the correct time format in JavaScript for this.

stormec
  • 179
  • 1
  • 10

1 Answers1

20

The lastmod tag uses YYYY-MM-DDThh:mmTZD format, where TZD is the time zone offset. The W3 date and time format gives you 3 options for how to do the TZD: (Z or +hh:mm or -hh:mm)

This means that in javascript you can just use

const date = new Date().toISOString();

and it'll look like 2017-11-15T11:18:17.266Z, which is correct for a sitemap lastmod.

Carasel
  • 2,740
  • 5
  • 32
  • 51
  • How can we show in +hh:mm or -hh:mm instead of Z? Official documentation uses +hh:mm format. https://www.google.com/sitemaps/protocol.html – Hasan Sefa Ozalp Jan 10 '21 at 00:07