Hello, even I was still thinking of a bomb-proof solution.
I believe that if the page seds a value that for me is well-defined, I am able to calculate how many minutes has been modified by the POST method.
So the project could be modified by inserting a hidden field that contains the date 2020-01-01 (for example) and that is sent as a parameter.
Evaluating the offset of the value received with the date 2020-01-01 I can reconstruct the value that the user on the other side of the world would have liked to send me.
Changes are in lines 29-34, 47, 64-68
using System;
using System.Collections.Generic;
using System.Globalization;
using Ext.Net;
using Ext.Net.Core;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace ExtCookbook.Pages
{
public class DateTimeZoneHandlingModel : PageModel
{
public FieldContainer MainContainer { get; set; }
public void OnGet()
{
MainContainer = new FieldContainer()
{
Anchor = "100%"
};
MainContainer.Items.Add(new DateField()
{
Id = "myDateField"
});
MainContainer.Items.Add(new TimeField()
{
Id = "myTimeField"
});
MainContainer.Items.Add(new DateField()
{
Id = "zuluTime",
Value = new DateTime(2020, 1, 1).ToShortDateString(),
FieldLabel = "zulu time offset calculator",
});
var btn = new Button()
{
Text = "Click me"
};
btn.DirectEvents.Click.Method = HttpMethod.POST;
btn.DirectEvents.Click.Url = $"?handler=ClickMeButtonClick";
btn.DirectEvents.Click.ExtraParams.Add(new DirectEventParameter() { Key = "myDate", Value = "App.myDateField.getValue()", Mode = ParameterMode.Raw });
btn.DirectEvents.Click.ExtraParams.Add(new DirectEventParameter() { Key = "myTime", Value = "App.myTimeField.getValue()", Mode = ParameterMode.Raw });
btn.DirectEvents.Click.ExtraParams.Add(new DirectEventParameter() { Key = "myDateString", Value = "App.myDateField.getRawValue()", Mode = ParameterMode.Raw });
btn.DirectEvents.Click.ExtraParams.Add(new DirectEventParameter() { Key = "myTimeString", Value = "App.myTimeField.getRawValue()", Mode = ParameterMode.Raw });
btn.DirectEvents.Click.ExtraParams.Add(new DirectEventParameter() { Key = "zuluTime", Value = "App.zuluTime.getValue()", Mode = ParameterMode.Raw });
MainContainer.Items.Add(btn);
}
public IActionResult OnPostClickMeButtonClick(JsObject jObj)
{
string postedDateValue = jObj.GetValueOrDefault("myDate").Value.ToString();
string postedTimeValue = jObj.GetValueOrDefault("myTime").Value.ToString();
var date = DateTime.Parse(postedDateValue, CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal);
var time = DateTime.Parse(postedTimeValue, CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal);
var dateString = DateTime.Parse(postedDateValue);
var timeString = DateTime.Parse(postedTimeValue);
DateTime postedZuluTimeValue = DateTime.Parse(jObj.GetValueOrDefault("zuluTime").Value.ToString());
var Offset = Microsoft.VisualBasic.DateAndTime.DateDiff(Microsoft.VisualBasic.DateInterval.Minute, new DateTime(2020, 01, 01), postedZuluTimeValue);
var realDate = DateTime.Parse(postedDateValue).AddMinutes(-Offset);
var realTime = DateTime.Parse(postedTimeValue).AddMinutes(-Offset);
this.X().Toast(@$"Posted date value is {postedDateValue} and converted to local time is {date.ToShortDateString()}; string value is {dateString}<br />
Posted date value is {postedTimeValue} and converted to local time is {time.ToShortTimeString()}; string value is {timeString}<br />
<b>Offset in minutes is {Offset}</b></br>
Submitted date value is <b>{realDate.ToShortDateString()}</b> and time is <b>{realTime.ToShortTimeString()}</b>");
return this.Direct();
}
}
}
In this example, DateField is not hidden, server clock is GMT+2 and client clock is GMT+8.
And the result is in this image:
About conversion from string I fear to get different format depending on the user local settings.
I mean, here in Italy date to string is dd/MM/yyyy, but in US is MM/dd/yyyy.
I don't know if 01/02/2003 is 1st of february or 2nd of january.