0

I have an input field that specifies the date/time that an item was shipped.

<div class="col-md-4">
    <div class="form-group">
        <label asp-for="TransloadingDetail.DateShipped" class="control-label"></label>
        <input asp-for="TransloadingDetail.DateShipped" class="form-control" />
        <span asp-validation-for="TransloadingDetail.DateShipped" class="text-danger"></span>
    </div>
</div>

I would like to set the default value for this field to the current date and time in the user's time zone.

My first thought was to set it in code on the server:

public IActionResult OnGet()
{
    TransloadingDetail = new() { DateShipped = DateTime.Now };
    return Page();
}

The problem is, this will use the date/time in the server's time zone.

So then I thought about using JavaScript/jQuery. Apparently, new Date() does not return the date in the correct format. Also, this article says that new Date() returns UTC, which I'm not using.

How can I initialize this field to the date/time using the user's time zone?

Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
  • 1
    Does this answer your question? [Getting the client's time zone (and offset) in JavaScript](https://stackoverflow.com/questions/1091372/getting-the-clients-time-zone-and-offset-in-javascript), assuming you're using JavaScript. – gunr2171 Sep 01 '21 at 16:33
  • @gunr2171: I don't see where it talks about populating the input tag. – Jonathan Wood Sep 01 '21 at 16:37

0 Answers0