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

6

34

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?

Consignee answered 10/7, 2015 at 20:2 Comment(0)
Z
36

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.

Zeb answered 11/7, 2015 at 7:3 Comment(6)
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.Consignee
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.Sentimentalize
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).Fausta
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?Vatic
@Sentimentalize this is a very late reply, but I wanted to note that @Zeb 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.Ikeikebana
@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.Sentimentalize
F
6

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

var ss = DateTime.Now.ToString("yyyy-mm-ddThh:mm:ss:zzz");
Fechner answered 11/10, 2017 at 12:46 Comment(1)
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");Blankbook
B
5

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.

Blankbook answered 10/8, 2018 at 3:27 Comment(0)
M
3

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
Murrain answered 22/2, 2018 at 9:36 Comment(2)
date('Y-m-d\TH:i:sP') more accurate.Relaxation
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.phpAmrita
W
3

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

Whaleback answered 29/3, 2019 at 20:32 Comment(0)
G
1

PHP should actually be:

date('Y-m-d\TH:i:s+00:00');
Gosling answered 12/6, 2018 at 23:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.