How to properly format last modified (lastmod) time for xml sitemaps

30,697

Solution 1

The lastmod tag is optional in sitemaps and in most of the cases it's ignored by search engines, because webmasters are doing a horrible job keeping it accurate. In any case, you may use it, and the format depends on your capabilities and requirements; you don't actually have to provide a timezone offset if you can't or don't want to, you can choose to go with a simple YYYY-MM-DD as well.

From the Lastmod definition section of sitemaps.org:

The date of last modification of the file. This date should be in W3C Datetime format. This format allows you to omit the time portion, if desired, and use YYYY-MM-DD.

If you want to go down to that granularity and provide the timezone offset as well, you're correct, it's UTC +/-. From W3C Datetime:

Times are expressed in local time, together with a time zone offset in hours and minutes. A time zone offset of "+hh:mm" indicates that the date/time uses a local time zone which is "hh" hours and "mm" minutes ahead of UTC. A time zone offset of "-hh:mm" indicates that the date/time uses a local time zone which is "hh" hours and "mm" minutes behind UTC.

And example, still from W3C:

1994-11-05T08:15:30-05:00 corresponds to November 5, 1994, 8:15:30 am, US Eastern Standard Time.

Solution 2

Properly format last modified (lastmod) time for XML sitemaps in C#

var ss = DateTime.Now.ToString("yyyy-mm-ddThh:mm:ss:zzz");

Solution 3

The format for the lastmod field in Google Sitemaps XML for C# is the following:

var lastmod = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:sszzz");

It provides values such as <lastmod>2018-08-24T09:18:38-04:00</lastmod> which is the W3C Datetime format.

Solution 4

In PHP, you can use :

$lastmod = date("Y-m-d\Th:m:s+00:00");

This will display something like:

2018-02-14T08:02:28+00:00

Solution 5

Date.prototype.toISOString()

The toISOString() method returns a string in simplified extended ISO format (ISO 8601), which is always 24 or 27 characters long (YYYY-MM-DDTHH:mm:ss.sssZ or ±YYYYYY-MM-DDTHH:mm:ss.sssZ, respectively). The timezone is always zero UTC offset, as denoted by the suffix "Z".

// expected output: 2011-10-05T14:48:00.000Z

Share:
30,697

Related videos on Youtube

petebolduc
Author by

petebolduc

Updated on April 13, 2021

Comments

  • petebolduc
    petebolduc about 3 years

    I am creating an app that will automatically update sitemap.xml each time new content is added to, or updated on the site.

    According to Google's best practices the <lastmod></lastmod> tag should be formatted as follows:

    <lastmod>2011-06-27T19:34:00+01:00</lastmod>

    My question concerns the time formatting itself. I understand the 2011-06-27T19:34:00 part. What I do not understand is the +01:00, which I am assuming is the +/- UTC.

    Is this a correct assumption?

    My Time Zone Table looks like this:

    enter image description here

    So if the site was based in #4 Afghanistan the correct time would be: 2011-06-27T19:34:00+04:00

    And if the site was based in #6 Alaska Standard Time the correct time would be: 2011-06-27T19:34:00-09:00

    Is my assumption correct or am I not correctly understanding the +01:00?

  • petebolduc
    petebolduc almost 9 years
    Thank you for you detailed explanation. I had read the section on sitemaps,org and realized the time was optional within the option of <lastmod> but i figured if my assumption of the UTC +/- was correct and the fact that I already had the info in the db table why not use it. I did not want to open a can of worms and have to go back and reprogram if my assumption was incorrect, which you have clarified for me... thanks again for taking the time for your detailed answer.
  • Peter
    Peter almost 7 years
    Google's published best practices (2014) explicitely say that "the two most important pieces of information for Google are the URL itself and its last modification time" and then explains what lastmod is expected to mean. That doesn't sound like they ignore it in most cases.
  • truemedia
    truemedia almost 6 years
    This is incorrect and displays invalid dates, the proper format for C# is the following: var lastmod = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:sszzz");
  • musa
    musa over 5 years
    date('Y-m-d\TH:i:sP') more accurate.
  • Brian C
    Brian C over 4 years
    My question in response to this answer (current version at the time I write is from 2015), would be - is it still the case in 2019 that search engines mostly ignore lastmod? I can imagine a strategy would be to derive it's trustworthiness for each site on an individual basis (ie has lastmod changed without a page update, etc). It seems worth the effort for us to have a reasonable stab at getting it as right as possible, both in terms of just being accurate and in case it starts to be used more over time (nothing authoritative about that, just an opinion).
  • cbdeveloper
    cbdeveloper over 4 years
    Let's say I don't change the visible (from user's point of view) contents of my 1000 product pages. But I added some new structured data tags (which I know crawlers do care about). So all of my 1000 product pages now are rendering some new (invisible to the user) data tags. Should I update the <lastmod> for all of those 1000 pages so googlebot can know that I need them re-index?
  • bsplosion
    bsplosion over 2 years
    @Peter this is a very late reply, but I wanted to note that @methode is/was a Google employee (Webmaster Trends Analyst at Google Switzerland). When he says lastmod is mostly ignored, it's a statement from authority. Obviously that was 2015, so perhaps webmasters do better these days and it's used again? Otherwise it does seem safe to ignore that attribute.
  • Peter
    Peter over 2 years
    @bsplosion, FWIW, I got pretty much the same comment on my question on the webmasters stackexchange in 2017 (quoting Gary Illyes, another Google employee), however Google's online documentation -- last updated in August '21 -- currently says "Google uses the <lastmod> value if it's consistently and verifiably (for example by comparing to the last modification of the page) accurate." So I suppose both statements are correct under the right circumstances.
  • Anthony Eden
    Anthony Eden about 2 years
    To further @musa's comment, the definition of "P" is 'Difference to Greenwich time (GMT) with colon between hours and minutes'. php.net/manual/en/datetime.format.php