1

I'm posting from Angular 6 to Asp Core api,object has one field Date type ,before posting it is shown in console:

Mon Jul 16 2018 00:00:00 GMT+0300 (Eastern European Summer Time)

but in Asp Core api is shown as {15/07/2018 9:00:00 PM}, 3 hours late and one day off,how to fix that?

thanks

mrapi
  • 5,831
  • 8
  • 37
  • 57
  • 1
    Its because in the AspNet Core API it converts the time to UTC time, so you need to convert it back to (Eastern European Summer Time) – Smokey Dawson Jul 16 '18 at 07:18
  • and how I can do that? – mrapi Jul 16 '18 at 07:21
  • 1
    What really happens is that JSON date encoding - which angular `HttpClient` module makes use when it has to make a POST request - transforms a date to UTC date. – korteee Jul 16 '18 at 07:34
  • 1
    in your asp core api add the time your time zone is ahead of the UTC, it should work fine. Or just use UTC everywhere which is preferred. – Roopesh Kumar Ramesh Jul 16 '18 at 07:37
  • 1
    @mrapi an easy way to do it would be to use the JavaScript library moment, there’s a function that converts utc time to your local time – Smokey Dawson Jul 16 '18 at 07:40
  • Hi,I've added in controller,but not working: ob.DATA = TimeZoneInfo.ConvertTimeFromUtc(ob.DATA, TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time")); – mrapi Jul 16 '18 at 07:42
  • @Roopesh how to use UTC everywhere? – mrapi Jul 16 '18 at 07:46
  • 1
    your asp.net core is already receiving time in utc format. when you're getting data back to the front end, like @SmokeyDawson said use javascript methods to convert utc to your local time. – Roopesh Kumar Ramesh Jul 16 '18 at 07:49
  • I've used this function: https://stackoverflow.com/a/47441370/3492996 – mrapi Jul 16 '18 at 08:59
  • 1
    If you check [this](https://savvytime.com/converter/eest-to-utc) website and input 16 July midnight EEST, you can see the UTC for that date is 15 July, 9pm. So as other users have commented, you can convert the source date to UTC using awesome library like [moment](https://momentjs.com/) or convert the date back to EEST in your backend using code such as `TimeZoneInfo.ConvertTimeFromUtc(yourTime, TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time"));` – Mausam Jul 16 '18 at 09:41

0 Answers0