最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

angular - How to ignore javascript timezone to send a datetime to server - Stack Overflow

programmeradmin1浏览0评论

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
Add a ment  | 

3 Answers 3

Reset to default 4

If 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.

发布评论

评论列表(0)

  1. 暂无评论