Oliver Booth
0a9c2e82d5
CORS was "cors"ing some issues (heh). But also it is easier to maintain this way. Development was made much more difficult when I separated it. Combining it all also improves SEO
34 lines
884 B
C#
34 lines
884 B
C#
using System.Globalization;
|
|
using SmartFormat.Core.Extensions;
|
|
|
|
namespace OliverBooth.Formatting;
|
|
|
|
/// <summary>
|
|
/// Represents a SmartFormat formatter that formats a date.
|
|
/// </summary>
|
|
public sealed class DateFormatter : IFormatter
|
|
{
|
|
/// <inheritdoc />
|
|
public bool CanAutoDetect { get; set; } = true;
|
|
|
|
/// <inheritdoc />
|
|
public string Name { get; set; } = "date";
|
|
|
|
/// <inheritdoc />
|
|
public bool TryEvaluateFormat(IFormattingInfo formattingInfo)
|
|
{
|
|
if (formattingInfo.CurrentValue is not string value)
|
|
return false;
|
|
|
|
if (!DateTime.TryParseExact(value, "yyyy-MM-dd",
|
|
CultureInfo.InvariantCulture,
|
|
DateTimeStyles.None,
|
|
out DateTime date))
|
|
return false;
|
|
|
|
|
|
formattingInfo.Write(date.ToString(formattingInfo.Format?.ToString()));
|
|
return true;
|
|
}
|
|
}
|