[英]How do I fix the time difference between MomentJS and Asp.net Core
I have a date that is set in my UI from this angularjs date picker . 我在这个angularjs 日期选择器的 UI中设置了一个日期。 It is set as a moment.js object and it includes a GMT offset for my timezone (Eastern). 它被设置为moment.js对象,它包含我的时区(东部)的GMT偏移量。 When I submit the moment object back to my Asp.net Core endpoint, the time is converted to the time with no offset. 当我将moment对象提交回我的Asp.net Core端点时,时间将转换为没有偏移的时间。 So for example, when I inspect date in the javascript moment object object: 例如,当我在javascript矩形对象对象中检查日期时:
timeIn: Fri Dec 16 2016 05:00:00 GMT-0500 (Eastern Standard Time)
And when I inspect my object in .net core: 当我在.net核心中检查我的对象时:
TimeIn: 12/16/2016 10:00:00AM
I want the data on the back end to show as the exact time entered on the UI, regardless of timezone. 我希望后端的数据显示为在UI上输入的确切时间,无论时区如何。 So if they enter 5:00AM, the time backend should store is 5:00AM. 因此,如果他们进入凌晨5点,应该存储的时间后端是凌晨5点。 It appears to change the value when the moment object is converted to JSON to send to the backend. 当moment对象转换为JSON以发送到后端时,它似乎会更改值。 When I do a JSON.stringify() of my timeIn object on the UI, I get: 当我在UI上的timeIn对象中执行JSON.stringify()时,我得到:
"2016-12-16T10:00:00.000Z"
Is there a way to strip the offset data from the timezone on the front end so that the time entered is the exact same as the time sent? 有没有办法从前端的时区剥离偏移数据,以便输入的时间与发送的时间完全相同?
PS I know there are a lot of questions out there about time but I wasn't able to find a suitable one for my case. PS我知道那里有很多关于时间的问题,但我找不到合适的问题。 Thanks for your help 谢谢你的帮助
EDIT: I am using .net DateTime object to accept the javascript date object. 编辑:我使用.net DateTime对象来接受javascript日期对象。
This format contains 5 spaces: 此格式包含5个空格:
Fri Dec 16 2016 05:00:00 GMT-0500 (Eastern Standard Time)
And we can assume 20
to be a minimum, safe entry for landing in the year. 我们可以假设20
是本年度降落的最低安全入口。 Get the next occurrence of space character and split from there. 获取下一个空格字符并从那里拆分。
Note: This one is totally based on assumptions, but it looks like it works better. 注意:这个完全基于假设,但看起来效果更好。
You can do something like: 你可以这样做:
var time = "Fri Dec 16 2016 05:00:00 GMT-0500 (Eastern Standard Time)"; console.log(time.substr(0, time.indexOf(" ", 20)));
Also, with reference to How to get the nth occurrence in a string? 另外,参考如何在字符串中获取第n次出现? , we can achieve something better. ,我们可以做得更好。
function getPosition(string, subString, index) {
return string.split(subString, index).join(subString).length;
}
We need the 5th occurrence of the space: 我们需要第5次出现的空间:
function getPosition(string, subString, index) { return string.split(subString, index).join(subString).length; } var time = "Fri Dec 16 2016 05:00:00 GMT-0500 (Eastern Standard Time)"; console.log(time.substr(0, getPosition(time, " ", 5)));
The second method seems to be a better one than the first. 第二种方法似乎比第一种方法更好。
The third method is to use the Moment.js library to format the time into a format accepted by .Net: 第三种方法是使用Moment.js库将时间格式化为.Net接受的格式:
timeIn = timeIn.format('MM-DD-YYYY h:mm:ss');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.