Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question for Binders and Converters Date&Time #1320

Closed
dc0106 opened this issue Aug 27, 2016 · 10 comments
Closed

Question for Binders and Converters Date&Time #1320

dc0106 opened this issue Aug 27, 2016 · 10 comments
Assignees
Labels

Comments

@dc0106
Copy link

dc0106 commented Aug 27, 2016

Hi @hikalkan,

According to Abp document :

Binders and Converters
  • ABP automatically normalizes DateTimes received from clients in MVC, Web API and ASP.NET Core applications, based on the current clock provider.
  • ABP automatically normalized DateTimes received from database based on the current clock provider, when EntityFramework or NHibernate modules used.

For #1, Abp can normalizes DateTime received from Clients. But it is no use for me.

In my UI as shown below

image

it can not be converted to UTC date&time to save to my database. Maybe I missed something. Please give me some suggestions, thanks.

I added this code in my project :

it is in Application_Start

Clock.Provider = new UtcClockProvider();
@dc0106
Copy link
Author

dc0106 commented Aug 30, 2016

Has anyone encountered this problem and is there a way to fix it?

@ismcagdas
Copy link
Member

Can you try with google chrome and check the datetime value for your request in network tab of developers console ?

If we can see the value send to server in ajax request, we can better understand the problem.

@dc0106
Copy link
Author

dc0106 commented Aug 30, 2016

Hi @ismcagdas ;
Thanks for your replay.

8-30-2016 3-16-12 pm

Abp cannot (Automatically) convert client datetime to UTC time. But I can use monent timezone plugins to convert datatime to I want (my local or any timezone).

I thinks the start date&time(2016-08-29 12:42) or end date&time should be convernt UTC time to save to database. But it does not do it. It is still (2016-08-29 12:42) in database.

Abp Document has a description :

ABP automatically normalizes DateTimes received from clients in MVC, Web API and ASP.NET Core applications, based on the current clock provider.

Maybe I missed something, If I am wrong, please give me some tips.

@ismcagdas
Copy link
Member

Hi,

Thanks for detailed screenshots.

When you send a datetime value without timezone information like this one "2016-08-29 16:49:00", it's Kind property is set to Unspecified and when it comes to UtcClockProvider's normalize method we only set it's Kind property to UTC. We dont convert it to UTC datetime because we cannot know it's original value is either local or utc.

I suggest you to send a datetime value to server in UTC or local format like '2016-08-29T10:49:00Z' or '2016-08-29T16:49+600'.

I think if you get the datetime value of your datepicker, it should give you something like this.

@dc0106
Copy link
Author

dc0106 commented Aug 30, 2016

Hi @ismcagdas ;

Thanks for your replay. I will try it as your suggestions.

Could Abp do it like this:

  1. We can save all clients input (Just DateTime fields ) as UTC datetime to database.
  2. Giving a setting for user to set his own timezone. (Per user has his own timezone)
  3. According to Permission based authorization system #2 , we can get user's timezone and use it to convert all datetime to user's local datetime from server(database)

@ismcagdas
Copy link
Member

  1. We only dont do it if kind property of datetime is set to Unspecified by MVC. When the kind is unspecified, we cannot know if it's original value is UTC or Local.
  2. Abp has a setting for that (setting name is Abp.Timing.TimeZone). You can use it in your app to keep timezone for each user. It's default value is UTC.
  3. I think this is not a good idea. It's better to use UTC/Original dates on the server side operations. You just need to convert dates to user's timezone when you are going to show it to user on your UI.

Which datepicker do you use by the way ?

And I suggest you to use momentjs timezone library (http://momentjs.com/timezone/) if you are going to use timezone setting for each user.

You just need to set it's default timezone value to user's timezone setting.
moment.tz.setDefault(abp.timing.timeZoneInfo.iana.timeZoneId);

@dc0106
Copy link
Author

dc0106 commented Aug 30, 2016

I used two kinds of datepicker :

  1. bootstrap-datetimepicker Use it to pick date.
  2. periodpicker Use it to pick Date & Time, my customer likes it.

BTW,SO all datetimes convert to local datetime (User's timezone) are in client-side. Is it right?

@ismcagdas
Copy link
Member

I have take a look at the periodpicker and it has a "value" method and it returns the datetime value( jQuery('#elementId').periodpicker('value') ). You can try to send this value to server.

BTW,SO all datetimes convert to local datetime (User's timezone) are in client-side. Is it right?

If you use UtcClockProvider, datetime values are sent to client in UTC. Then you need to convert this datetime to user's timezone in the client. ABP does not do anything automatically.

You can use TimezoneHelper.ConvertFromUtc method in your MVC views or abp.timing.convertToUserTimezone javascript helper function (if you are using momentjs timezone library) to convert an UTC date to user's timezone.

abp.timing.convertToUserTimezone javascript function is defined here https://github.com/aspnetboilerplate/aspnetboilerplate/blob/dev/src/Abp.Web.Resources/Abp/Framework/scripts/libs/abp.moment.js

@dc0106
Copy link
Author

dc0106 commented Aug 30, 2016

Hi @ismcagdas,
Thanks for your help. I understood completely. I think Timezone feature does not describe very clearly in ABP document.

To use TimezoneHelper.ConvertFromUtc method I will implement a custom AbpSession to get TimezoneId for each user. if he has his own timezone.

Thk your very much again.

@ismcagdas
Copy link
Member

We will try to improve timezone documentation. Thanks for your suggestion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants