简体   繁体   English

减去日期时间值

[英]Subtracting Date time value

I have the upload date for a course saved in a ViewModel variable @Model.Course.UploadDate when calling the following code: 在调用以下代码时,我有一个保存在ViewModel变量@ Model.Course.UploadDate中的课程的上传日期:

alert('@Model.Course.UploadDate');

I get an output as expected of: 我得到了预期的输出:

21/01/2014 16:16:13

I know want to check that the uploadDate is within the last 10 seconds before sending a statement to the database but trying to use the following code: 我知道想要在向数据库发送语句之前检查uploadDate是否在最后10秒内但是尝试使用以下代码:

var uploadDate = new Date('@Model.Course.UploadDate.ToLongDateString()');
alert("UPLOAD DATE " + uploadDate);

I get an unexpected output of: 我得到了一个意想不到的输出:

Tue Jan 21 2013 00:00:00 GMT+0000

This is the format that I need the date in only with the saved time data shown. 这是我需要日期的格式,只显示所保存的时间数据。 I am then looking to perform a calculation as follows: 我正在寻找如下计算:

var TENSECONDS = 10 * 1000;
var uploadDate = new Date('@Model.Course.UploadDate.ToLongDateString()');
var today = new Date();
var check = today - uploadDate;

if (parseInt(check) > parseInt(TENSECONDS))
        alert("ROUTE1");
    else
        alert("ROUTE2");

Quote from the documentation of the Date object constructor: 引用Date对象构造函数的documentation

value: Integer value representing the number of milliseconds since 1 January 1970 00:00:00 UTC (Unix Epoch). value:整数值,表示自1970年1月1日00:00:00 UTC(Unix Epoch)以来的毫秒数。

So actually that's the safest thing to pass to the constructor of a Date object instead of some strings which might be incorrectly interpreted and are completely culture dependent. 实际上,传递给Date对象的构造函数而不是一些可能被错误解释并完全依赖于文化的字符串是最安全的。

So just convert your DateTime instance to the number of milliseconds that elapsed since 1 January 1970 and feed this timestamp to the constructor: 因此,只需将DateTime实例转换为自1970年1月1日以来经过的毫秒数,并将此时间戳提供给构造函数:

var timestamp = @(Model.Course.UploadDate - new DateTime(1970, 1, 1)).TotalSeconds;
var uploadDate = new Date(timestamp);

As an alternative you could use the ISO8601 format if you intend to be passing a string: 作为替代方案,如果您打算传递字符串,则可以使用ISO8601格式:

dateString: String value representing a date. dateString:表示日期的字符串值。 The string should be in a format recognized by the Date.parse() method (IETF-compliant RFC 2822 timestamps and also a version of ISO8601). 字符串应采用Date.parse()方法识别的格式(符合IETF的RFC 2822时间戳和ISO8601的版本)。

So: 所以:

var uploadDate = new Date('@Model.Course.UploadDate.ToString("o")');

I solved this using the following code: 我使用以下代码解决了这个问题:

var dateArray = new Array();
dateArray = '@Model.Course.UploadDate'.split("/");
var dateD = dateArray[0];
var dateM = dateArray[1];
var dateY = dateArray[2];
var dateT = dateArray[3];
timeArray = dateT.split(":");
var timeH = timeArray[0];
var timeM = timeArray[1];
var timeS = timeArray[2];
var dateUS = dateM + "/" + dateD + "/" + dateY + dateT;
var uploadDate = new Date(dateD,dateM,dateY,timeH,timeM,timeS);

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM