2

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?

4
  • Possible duplicate of How do I get a UTC Timestamp in JavaScript? 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. Commented Nov 15, 2017 at 10:20
  • Are you using html5 datetime input? Commented Nov 15, 2017 at 10:22
  • It's an angular date picker. Commented Nov 16, 2017 at 5:35

3 Answers 3

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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Or use config.Formatters.JsonFormatter.SerializerSettings.DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.RoundtripKind; //Time zone information should be preserved when converting.
3

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')

1 Comment

Yes moment with first format did work there. Actually a given string format without timezone info rather than date time object are workable there. And this same thing are applicable too when i sending back time information from server to client. Need to send a specific string format rather than date time object. Thanks.
2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.