Sitemap LastMod property serialized in an incorrect format
geogauci opened this issue · comments
Google's webmasters tools is throwing several errors on the LastMod property, "An invalid date was found. Please fix the date or formatting before resubmitting". Upon inspection, as per sitemaps.org, the date must be in a W3C format.
I believe a minor update in SitemapNode.cs such as the following should do the trick (PS: not tested):
/// <summary>
/// Shows the date the URL was last modified, value is optional.
/// </summary>
[XmlElement("lastmod", Order = 2)]
public string LastModificationDateW3C
{
get
{
if (this.LastModificationDate != null)
{
return XmlConvert.ToString((DateTime)this.LastModificationDate);
}
return null;
}
}
/// <summary>
/// The date the URL was last modified
/// </summary>
public DateTime? LastModificationDate { get; set; }
Could you share an example datetime generated in your sitemap?
Ok, I'll take a look this one and try to provide a fix today.
Thanks!
I see different behaviour on .NET and .NET Core.
Which one are you using?
.Net (4.6.1)
Your approach's also generating a weird format.
You can take a look at this branch:
https://github.com/uhaciogullari/SimpleMvcSitemap/compare/datetime-format-fix?expand=1
There are two other alternatives.
- Convert your value to UTC
- Create your DateTime explicitly as Local like below if you want to use specify your timezone
new DateTime(2016, 3, 3, 0, 0, 0, DateTimeKind.Local);
Could you test this and see if it works?
The first method (converting to UTC) does not work, however the second method works perfectly! :)
Just a recap, after retrieving the datetime required, I created a new DateTime, as per your second suggestion, like so:
DateTime lta = <get some date, _in local time zone_ >
DateTime w3cDateTime = new DateTime(lta.Year, lta.Month, lta.Day, lta.Hour, lta.Minute, lta.Second, DateTimeKind.Local);
sitemapNode.LastModificationDate = w3cDateTime;
Thanks for your support and for your awesome tool! :)
You're welcome.
It seems like there is a ToLocalTime method as well and it looks cleaner.