I need to display the datetime received in CET to the local time of user by using javascript in a .NET MVC application. How can this be done?
<table>
<thead>
<tr>
<th>Date</th>
</tr>
</thead>
<tbody>
<tr>
<td data-utc-time="@item.ScheduledTime?.ToUniversalTime().ToString("o")">@item.ScheduledTime.HasValue ? item.ScheduledTime.Value.ToUniversalTime() : "N/A"</td>
</tr>
</tbody>
</table>
function convertUtcToLocal() {
document.querySelectorAll('[data-utc-time]').forEach(function (element) {
const utcTime = element.getAttribute('data-utc-time');
if (utcTime) {
const localTime = new Date(utcTime).toLocaleString('en-GB', {
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
hour12: false
});
element.textContent = localTime;
}
});
}
This is the code I tried I added a class for each datetime and got the utc time using ToUniversalTime()
and then used ToLocaleString()
to convert the UTC time to local time of user but this does not work