我正在计算我的申请日期,如日期,星期,月份 .
首先我定义日,周,月,定制如:
this.reportTypes = [{TypeId: 1, Type: 'Day'}, {TypeId: 6, Type: 'Week'}, {TypeId: 30, Type: 'Month'}, {
TypeId: 10,
Type: 'Custom'
}]
接下来我定义的日期如下:
var currdate = new Date();
if(reportType==1){
// this.reportDataFromDate=currdate;
// this.reportDataToDate=currdate;
//This is for setting the current date
this.reportDataFromDate= currdate;
this.reportDataToDate= currdate;
}
else if(reportType==30){
var First = new Date(currdate.getFullYear(),currdate.getMonth(),1);
this.reportDataFromDate=First;
this.reportDataToDate=currdate;
}
else if(reportType!=10){
var last = new Date(currdate.getTime() - (reportType * 24 * 60 * 60 * 1000));
this.reportDataFromDate=last;
this.reportDataToDate=currdate;
}
}
问题是在选择 reportType == 30
之后它必须得到该月的第一天 .
它显示的日期为 1-Dec-2017
但是它将获取截至2017年11月30日的数据?
这是SQL服务器的屏幕截图 . 我发送日期为2017年12月1日,但它将在30-11-2017 .
3 回答
为什么不同?
new Date(x,y,z)
构造函数将参数视为 local date values .见MDB Web Docs - Date
但是,在引擎盖下,日期存储为UTC(自1970年1月1日以来的毫秒数) .
并且UTC日期与您的本地日期不同(请参阅尾随'Z'表示UTC)
当您将其发送到服务器时,JSON将其作为UTC发送
如何修复它
好吧,你可能会认为你确实想要在当地的日期/时间,并得出结论它没有被打破 .
但是如果你想将 send UTC发送到服务器,请将参数包装在
Date.UTC()
中月参数怎么样=== 11?
从上面引用的MDB页面,
当使用整数调用Date()构造函数时,结果是一个日期对象,该日期假定您的系统(读取浏览器/ os)时区 .
例:
这可能会在发送到服务器的整个不同年份结束
使用字符串构造函数并指定时区将帮助您克服这个问题 .
例:
后者是你应该传递给服务器的,通常做计算 .
但请注意,使用日期计算是一个复杂的问题,最好留给像moment.js这样的库 . 要了解您正在处理的内容,请查看WebRebel Session 中的great talk .
因此,要实际给出 Headers 的答案,请尝试使用UTC在简单字符串中创建日期的示例:
请注意,由于getUTCMonth()返回1月为0,我们添加1个月 .
如果您使用.Net Web API作为后端,则可以在Web API WebApiconfig.cs中配置时区,如下所示 . 它将以UTC格式化时间 .
或者使用