At the server-side, you can use a extension method, like this:
public static class DateTimeExtensions {
private static readonly long UnixEpochTicks = (new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).Ticks;
public static long? ToJavascriptTicks(this DateTime? value) {
return value == null ? (long?)null : (value.Value.ToUniversalTime().Ticks - UnixEpochTicks) / 10000;
}
public static long ToJavascriptTicks(this DateTime value) {
return (value.ToUniversalTime().Ticks - UnixEpochTicks) / 10000;
}
}
With this extensions, you can get the javascript ticks, and then you simply pass them to the client-side.
If you are using MVC:
You have the ViewModel:
public class MyViewModel {
public long MyJsTicks { get; set; }
}
And the Controller:
public ActionResult Action() {
long myJsTicks = DateTime.UtcNow.ToJavascriptTicks(); //<-- use the extension method
MyViewModel viewModel = new MyViewModel();
viewModel.MyJsTicks = myJsTicks;
return View(viewModel);
}
At the client-side:
var jsticks = <the js ticks obtained from server-side>;
var mydatetime = new Date(jsticks);
If you are using Razor view engine for your mobile app, getting the calculated js ticks from the server-side in your view is extremely simple, using a in-line expression:
var jsticks = @(Model.MyJsTicks);
var mydatetime = new Date(jsticks);
Finally, to get days, hours and minutes from the javascript Date object:
var hours = mydatetime.getHours();
var minutes = mydatetime.getMinutes();
var seconds = mydatetime.getSeconds();
(as you can see in the javascript "Date" object reference: https://www.w3schools.com/jsref/jsref_obj_date.asp)