How to properly format last modified (lastmod) tim

2020-06-08 02:53发布

问题:

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:

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?

回答1:

The lastmod tag is optional in sitmaps 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.



回答2:

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

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


回答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.



回答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


回答5:

PHP should actually be:

date('Y-m-d\TH:i:s+00:00');


回答6:

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



标签: xml sitemap