简体   繁体   English

如何修复MomentJS和Asp.net Core之间的时差

[英]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.

相关问题 如何修复或摆脱ASP.NET Razor应用程序中的ESLint定义要求? - How do I fix or get rid of ESLint definition requirement in ASP.NET Razor app? Javascript和ASP.net之间的区别 - Difference between Javascript and ASP.net 如何将数据以JSON格式从userData传输到饼图? ASP.Net核心 - How do I transfer data from userData in JSON format to a pie chart? ASP.Net Core 如何使用 ASP.NET Core 3 中的 Ajax 函数将 PartialView 插入到普通视图中? - How do I insert a PartialView into a normal View using an Ajax-Function in ASP.NET Core 3? 如何将视图中的 javascript 函数中的变量传递给 ASP.NET Core 中控制器中的操作 - How do I pass variables from a javascript function in a view to an action in the controller in ASP.NET Core 如何设置 ASP.NET 内核 + Vue.js? - How do I set up ASP.NET Core + Vue.js? 如何将自定义样式表和脚本添加到ASP.NET Core项目? - How do I add custom stylesheets and scripts to a ASP.NET Core project? 如何从asp.net核心视图更新javascript中的模型值 - How do I update model value in javascript from asp.net core view 如何在尝试使用 Razor Pages 删除 ASP.NET Core 中的记录时显示确认消息 - How do I display a confirmation message with trying to delete a record in ASP.NET Core using Razor Pages MateiralizeCSS Timepicker:如何使用momentJS查找两个时间选择器之间的时差,在AM / PM之前有一个空格? - MateiralizeCSS Timepicker: How to find the time difference between two time pickers, with a space before AM/PM, using momentJS?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM