From 63ec83266763257316f80f9f63f23509090d69e7 Mon Sep 17 00:00:00 2001 From: rockstardev Date: Sat, 26 May 2018 09:32:20 -0500 Subject: [PATCH] Support for localizing datetimes base on browser's timezone --- BTCPayServer/Controllers/InvoiceController.UI.cs | 2 +- BTCPayServer/Models/InvoicingModels/InvoicesModel.cs | 5 +---- BTCPayServer/Views/Invoice/ListInvoices.cshtml | 2 +- BTCPayServer/Views/ViewsRazor.cs | 8 ++++++++ BTCPayServer/bundleconfig.json | 3 ++- BTCPayServer/wwwroot/main/site.js | 9 +++++++++ 6 files changed, 22 insertions(+), 7 deletions(-) create mode 100644 BTCPayServer/wwwroot/main/site.js diff --git a/BTCPayServer/Controllers/InvoiceController.UI.cs b/BTCPayServer/Controllers/InvoiceController.UI.cs index 4b32fc9f5..0b04d6a36 100644 --- a/BTCPayServer/Controllers/InvoiceController.UI.cs +++ b/BTCPayServer/Controllers/InvoiceController.UI.cs @@ -425,7 +425,7 @@ namespace BTCPayServer.Controllers { Status = invoice.Status + (invoice.ExceptionStatus == null ? string.Empty : $" ({invoice.ExceptionStatus})"), ShowCheckout = invoice.Status == "new", - Date = (DateTimeOffset.UtcNow - invoice.InvoiceTime).Prettify() + " ago", + Date = invoice.InvoiceTime, InvoiceId = invoice.Id, OrderId = invoice.OrderId ?? string.Empty, RedirectUrl = invoice.RedirectURL ?? string.Empty, diff --git a/BTCPayServer/Models/InvoicingModels/InvoicesModel.cs b/BTCPayServer/Models/InvoicingModels/InvoicesModel.cs index 63385532d..5dfdd5260 100644 --- a/BTCPayServer/Models/InvoicingModels/InvoicesModel.cs +++ b/BTCPayServer/Models/InvoicingModels/InvoicesModel.cs @@ -33,10 +33,7 @@ namespace BTCPayServer.Models.InvoicingModels public class InvoiceModel { - public string Date - { - get; set; - } + public DateTimeOffset Date { get; set; } public string OrderId { get; set; } public string RedirectUrl { get; set; } diff --git a/BTCPayServer/Views/Invoice/ListInvoices.cshtml b/BTCPayServer/Views/Invoice/ListInvoices.cshtml index d6e6915f8..4d90ef99f 100644 --- a/BTCPayServer/Views/Invoice/ListInvoices.cshtml +++ b/BTCPayServer/Views/Invoice/ListInvoices.cshtml @@ -66,7 +66,7 @@ @foreach(var invoice in Model.Invoices) { - @invoice.Date + @invoice.Date.BrowserDate() @if(invoice.RedirectUrl != string.Empty) { diff --git a/BTCPayServer/Views/ViewsRazor.cs b/BTCPayServer/Views/ViewsRazor.cs index 76480739e..2a83b810e 100644 --- a/BTCPayServer/Views/ViewsRazor.cs +++ b/BTCPayServer/Views/ViewsRazor.cs @@ -1,7 +1,9 @@ using System; using System.Collections.Generic; +using System.Globalization; using System.Linq; using System.Threading.Tasks; +using Microsoft.AspNetCore.Html; using Microsoft.AspNetCore.Mvc.ViewFeatures; namespace BTCPayServer.Views @@ -22,5 +24,11 @@ namespace BTCPayServer.Views var activePage = (T)viewData[ACTIVE_PAGE_KEY]; return page.Equals(activePage) ? "active" : null; } + + public static HtmlString BrowserDate(this DateTimeOffset date) + { + var hello = date.ToString("o", CultureInfo.InvariantCulture); + return new HtmlString($"{hello}"); + } } } diff --git a/BTCPayServer/bundleconfig.json b/BTCPayServer/bundleconfig.json index 9d7b0ac6c..4e89d7ace 100644 --- a/BTCPayServer/bundleconfig.json +++ b/BTCPayServer/bundleconfig.json @@ -18,7 +18,8 @@ "wwwroot/vendor/jquery-easing/jquery.easing.js", "wwwroot/vendor/scrollreveal/scrollreveal.min.js", "wwwroot/vendor/magnific-popup/jquery.magnific-popup.js", - "wwwroot/vendor/bootstrap4-creativestart/*.js" + "wwwroot/vendor/bootstrap4-creativestart/*.js", + "wwwroot/main/**/*.js" ] }, { diff --git a/BTCPayServer/wwwroot/main/site.js b/BTCPayServer/wwwroot/main/site.js new file mode 100644 index 000000000..8a182c1ed --- /dev/null +++ b/BTCPayServer/wwwroot/main/site.js @@ -0,0 +1,9 @@ +$(function () { + $(".localizeDate").each(function (index) { + var serverDate = $(this).text(); + var localDate = new Date(serverDate); + + var dateString = localDate.toLocaleDateString() + " " + localDate.toLocaleTimeString(); + $(this).text(dateString); + }); +});