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

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

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

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.


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.

Tags:

Xml

Sitemap