简体   繁体   English

如何在ASP.NET MVC控制器参数中将毫秒转换为DateTime

[英]How to convert milliseconds to DateTime in ASP.NET MVC controller parameter

Gant chart passes time in milliseconds to MVC4 controller. 甘特图将时间(以毫秒为单位)传递给MVC4控制器。 Code below prints out 1440190800000 下面的代码打印出1440190800000

$(".gantt").gantt({
    onAddClick: function (dt, rowId) {
        alert(dt);
        window.location.href='NewBooking?' + $.param({
            datetime: dt,
            row: rowId
        });
    },

MVC4 controller has signature: MVC4控制器具有签名:

public ActionResult NewBooking(DateTime datetime, string row)
{
    var m = new NewBookingViewModel();
    return View(m);
}

Calling this controller causes error 调用此控制器会导致错误

The parameters dictionary contains a null entry for parameter 'datetime' of non-nullable type 'System.DateTime' for method 'System.Web.Mvc.ActionResult NewBooking(System.DateTime, System.String)' in 'Eeva.Erp.Controllers.BookingController'. An optional parameter must be a 
since milliseconds are not contverted to datetime.

How to fix this in controller code or in javascript to get DateTime value ? 如何在控制器代码或javascript中解决此问题以获得DateTime值?

Milliseconds cannot represent dates. 毫秒不能代表日期。 A millisecond is a unit for measuring time duration. 毫秒是用于测量持续时间的单位。 So asking how to convert time duration into a DateTime C# object doesn't make sense. 因此,询问如何将持续时间转换为DateTime C#对象没有任何意义。

On the other hand milliseconds elapsed since some fixed date in the time (like The Epoch ) can represent a DateTime. 另一方面,自时间中某个固定日期(例如The Epoch )可以表示DateTime以来经过的毫秒数。 I am not familiar with the client library you are using and what those milliseconds represent but let's assume for the purpose of this example that they represent the number of milliseconds elapsed since 1 January 1970 00:00:00 UTC time. 我不熟悉您使用的客户端库及其代表的毫秒数,但出于本示例的目的,我们假设它们代表自1970年1月1日UTC时间以来经过的毫秒数。 In this case you could simply convert it to the corresponding DateTime object: 在这种情况下,您可以简单地将其转换为相应的DateTime对象:

public DateTime FromUnixTime(long unixTime)
{
    var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
    return epoch.AddMilliseconds(unixTime);
}

and then: 接着:

public ActionResult NewBooking(long datetime, string row)
{
    DateTime someDate = FromUnixTime(datetime);
    var m = new NewBookingViewModel();
    return View(m);
}

Obviously this code can be further improved so that this conversion is made in a custom model binder and your controller action could then directly take a DateTime object parameter. 显然,可以进一步改进此代码,以便在自定义模型绑定程序中进行此转换,然后您的控制器操作可以直接采用DateTime对象参数。

So now it's really up to you and the documentation of the js library you are using to elaborate the precise algorithm for converting those milliseconds to a DateTime. 因此,现在取决于您和您所使用的js库的文档,以详细说明将这些毫秒转换为DateTime的精确算法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 asp.net MVC控制器无法识别DateTime url参数? - asp.net mvc controller does not recognize DateTime url parameter? controller中edit方法的transfer参数中的DateTime总是01.01.0001 00:00:00 asp.net mvc5 - DateTime in the transfer parameter of the edit method in the controller is always 01.01.0001 00:00:00 asp.net mvc5 在ASP.NET MVC中将客户端日期时间转换为服务器日期时间 - Convert a client datetime to server datetime in ASP.NET MVC 解析DateTime? 格式从ajax到asp.net MVC控制器 - Parse DateTime? format from ajax to asp.net mvc controller asp.net mvc-设置日期时间参数的语言环境/格式 - asp.net mvc - Set locale / format of a DateTime parameter ASP.NET无法将参数值从字符串转换为DateTime - ASP.NET Failed to convert parameter value from a String to a DateTime ASP.NET MVC 将 DateTime 转换为 UTC 发送到 API - ASP.NET MVC convert DateTime to UTC to send to API 如何将DateTime参数从View传递到ASP.net MVC5中的Controller? - How do I pass DateTime parameters from View to Controller in ASP.net MVC5? 如何解决无法将参数值从字符串转换为asp.net中的DateTime? - How To fix Failed to convert parameter value from a String to a DateTime in asp.net? ASP.NET MVC Wild Card Controller任何参数路由 - ASP.NET MVC Wild Card Controller any parameter routing
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM