admin管理员组文章数量:1326064
I am building a web app with c# and angular to store event schedule. The user will input a date time which is in UTC and I want to save that time in db exactly as inputted. And then show exactly same time when a user will view this information. And this should not depend on user's timezone, and I am telling the user that time is in UTC.
But the problem is browser send time information with timezone info when ajax post to server and server saves a different time (by converting its own timezone) in db as the server is in a different timezone.
How to prevent this?
I am building a web app with c# and angular to store event schedule. The user will input a date time which is in UTC and I want to save that time in db exactly as inputted. And then show exactly same time when a user will view this information. And this should not depend on user's timezone, and I am telling the user that time is in UTC.
But the problem is browser send time information with timezone info when ajax post to server and server saves a different time (by converting its own timezone) in db as the server is in a different timezone.
How to prevent this?
Share Improve this question edited Nov 15, 2017 at 11:37 Iman Bahrampour 6,8002 gold badges45 silver badges65 bronze badges asked Nov 15, 2017 at 10:07 Zakir HossainZakir Hossain 1081 silver badge6 bronze badges 4- Possible duplicate of How do I get a UTC Timestamp in JavaScript? – Dhyey Commented Nov 15, 2017 at 10:10
- May be no, as i want to send a date time without timezone info, i don't want to send it with UTC timezone info. Because when i send UTC time, server converts it to a new time if server not in UTC GMT 0 timezone. – Zakir Hossain Commented Nov 15, 2017 at 10:20
- Are you using html5 datetime input? – Karthik RP Commented Nov 15, 2017 at 10:22
- It's an angular date picker. – Zakir Hossain Commented Nov 16, 2017 at 5:35
3 Answers
Reset to default 4If you are using .Net Web API as backend, you can config the timezone in Web API WebApiconfig.cs like below. It will serialize the time in UTC.
public static void Register(HttpConfiguration config)
{
config.Formatters.JsonFormatter.SerializerSettings.DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Utc;
}
I am not sure how you are formatting. But I think you could use momentjs in case you don't want to use any custom solution. The following code should work
moment(your_date_time).format('YYYY-MM-DD HH:mm:ss')
in case your_date_time has time zone, then the following should work
moment().utc().format('YYYY-MM-DD HH:mm:ss')
So if you are using html5 datetime,then you can change it to datetime-local
<input type="datetime-local">
This will give you datetime without the timezone info.
Here is an example.
var dateControl = document.querySelector('input[type="datetime-local"]');
dateControl.value = '2017-06-01T08:30';
let btn = document.getElementById('btns');
let inp = document.getElementById('party');
let dv = document.getElementById('displayValue');
btn.addEventListener('click', ()=>{
dv.innerText = inp.value;
});
<label for="party">Enter a date and time for your party booking:</label>
<input id="party" type="datetime-local" name="partydate" value="2017-06-01T08:30">
<button type="submit" id="btns">Check Value</button>
<div id="displayValue">
</div>
Code snippet is just to give you an idea on how to use datetime-local. If you are using angular 2/4, then you can directly bind ngModel to a variable as you would do generally to get the value.
本文标签: angularHow to ignore javascript timezone to send a datetime to serverStack Overflow
版权声明:本文标题:angular - How to ignore javascript timezone to send a datetime to server - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742196881a2431241.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论