Create Time Condion in Razor with @Model.CreatedOn and @DateTime.Now

6 days ago
np4.6

i want to compare CreatedOn time with current User's Device time and if it is more than 1 day after creation day, then do some action. also the same but with 3 hours

i am trying to make a condition like:
@if(Model.CreatedOn < DateTime.Now.AddDays(-1)) {<div>show text</div>}
this works, but not as I needed

how can i do it?
5 days ago
RE: "this works, but not as I needed"
Please clarify.
5 days ago
The customer creation date which is saved in the database is in UTC.
You have to convert the user's current date time to UTC.
Then you can compare them.
Here is an example, this can be helpful.

--C#--
DateTime utcDateTimeFromDb = // Retrieve this from your database

-- JavaScript--
// Get current datetime in the user's local time zone
var localDateTime = new Date();

// Convert local datetime to UTC
var utcDateTimeNow = localDateTime.toISOString();
DateTime utcDateTimeNowFromBrowser = DateTime.Parse(utcDateTimeNow);

if (utcDateTimeFromDb > utcDateTimeNowFromBrowser)
{
    // Database datetime is later than the browser's current datetime
}
else if (utcDateTimeFromDb < utcDateTimeNowFromBrowser)
{
    // Database datetime is earlier than the browser's current datetime
}
else
{
    // Both datetimes are the same
}
5 days ago
New York wrote:
RE: "this works, but not as I needed"
Please clarify.


I want to show ManufacturerPartNumber on Order Details page only for first 3 hours after the order is paid. After 3 hours I want to hide ManufacturerPartNumber from client on Order Details page. This ManufacturerPartNumber will have some secret code, of course on all other pages ManufacturerPartNumber is hidden from client. Also I would like to know how to do the same but for 1 day.

Now I am using the following code:
@foreach (var item in Model.Items)
                            {
              var product = await productService.GetProductByIdAsync(item.ProductId);
                                <tr>
                                    @if (Model.ShowSku)
                                    {
                                        <td class="sku">
                                            <label class="td-title">@T("Order.Product(s).SKU"):</label>
                                            <span class="sku-number">@item.Sku</span>
                                        </td>
                                    }
                                    @if (Model.ShowProductThumbnail)
                                    {
                                        <td class="picture">
                                            <a href="@(Url.RouteUrl<Product>(new {SeName = item.ProductSeName}))"><img alt="@item.Picture.AlternateText" src="@item.Picture.ImageUrl" title="@item.Picture.Title" /></a>
                                        </td>
                                    }
                                    <td class="product">
                                        @if (!Model.PrintMode)
                                        {
                                            <em><a href="@(Url.RouteUrl<Product>(new { SeName = item.ProductSeName }))">@item.ProductName</a></em>
                      @if(Model.OrderStatus.Equals("Completed") && Model.PaymentMethodStatus.Equals("Paid") && ((DateTime.Now - Model.CreatedOn).TotalDays < 1)) {
                      <div class="xxcc">@product.ManufacturerPartNumber</div>
                      }

                                        }
                                        else
                                        {
                                            @item.ProductName
                                        }


It seems to work, but I am not sure which Date-time is getting with @DateTime.Now? Time From User's PC or from my VPS server DateTime?
5 days ago
ashifahmed wrote:
The customer creation date which is saved in the database is in UTC.
You have to convert the user's current date time to UTC.
Then you can compare them.
Here is an example, this can be helpful.

--C#--
DateTime utcDateTimeFromDb = // Retrieve this from your database

-- JavaScript--
// Get current datetime in the user's local time zone
var localDateTime = new Date();

// Convert local datetime to UTC
var utcDateTimeNow = localDateTime.toISOString();
DateTime utcDateTimeNowFromBrowser = DateTime.Parse(utcDateTimeNow);

if (utcDateTimeFromDb > utcDateTimeNowFromBrowser)
{
    // Database datetime is later than the browser's current datetime
}
else if (utcDateTimeFromDb < utcDateTimeNowFromBrowser)
{
    // Database datetime is earlier than the browser's current datetime
}
else
{
    // Both datetimes are the same
}


Thanks you so much for the code.
Could you please write how to make the rule for showing smth for only 3 hours or 1 day?
5 days ago
danielaguero wrote:
RE: "this works, but not as I needed"
Please clarify.


I want to show ManufacturerPartNumber on Order Details page only for first 3 hours after the order is paid. After 3 hours I want to hide ManufacturerPartNumber from client on Order Details page. This ManufacturerPartNumber will have some secret code, of course on all other pages ManufacturerPartNumber is hidden from client. Also I would like to know how to do the same but for 1 day.

Now I am using the following code:
@foreach (var item in Model.Items)
                            {
              var product = await productService.GetProductByIdAsync(item.ProductId);
                                <tr>
                                    @if (Model.ShowSku)
                                    {
                                        <td class="sku">
                                            <label class="td-title">@T("Order.Product(s).SKU"):</label>
                                            <span class="sku-number">@item.Sku</span>
                                        </td>
                                    }
                                    @if (Model.ShowProductThumbnail)
                                    {
                                        <td class="picture">
                                            <a href="@(Url.RouteUrl<Product>(new {SeName = item.ProductSeName}))"><img alt="@item.Picture.AlternateText" src="@item.Picture.ImageUrl" title="@item.Picture.Title" /></a>
                                        </td>
                                    }
                                    <td class="product">
                                        @if (!Model.PrintMode)
                                        {
                                            <em><a href="@(Url.RouteUrl<Product>(new { SeName = item.ProductSeName }))">@item.ProductName</a></em>
                      @if(Model.OrderStatus.Equals("Completed") && Model.PaymentMethodStatus.Equals("Paid") && ((DateTime.Now - Model.CreatedOn).TotalDays < 1)) {
                      <div class="xxcc">@product.ManufacturerPartNumber</div>
                      }

                                        }
                                        else
                                        {
                                            @item.ProductName
                                        }


It seems to work, but I am not sure which Date-time is getting with @DateTime.Now? Time From User's PC or from my VPS server DateTime?


In that case, just use the UTC time when pulling user's date time as the created time is in UTC.

//for 1 day
@if (Model.CreatedOn >= DateTime.UtcNow.AddDays(-1))
{
    <div>show text</div>
}

//for 3 hours
@if (Model.CreatedOn >= DateTime.UtcNow.AddHours(-3))
{
    <div>show text</div>
}
4 days ago
"CreatedOn" in the View Models have already been converted to user's time:

\Presentation\Nop.Web\Factories\OrderModelFactory.cs
PrepareOrderDetailsModelAsync
CreatedOn = await _dateTimeHelper.ConvertToUserTimeAsync(order.CreatedOnUtc, DateTimeKind.Utc),


You're already using services in your View
  productService.GetProductByIdAsync(...
I recommend that you use a service to get the original Order itself and use its CreatedOnUtc
var order = await orderService.GetOrderByIdAsync(Model.Id);

(you'll need to add 'header' code to resolve the service)