DateTime handling in AngularJS and WebAPI - angularjs

I am working on a new application which already having data. The dates in database are stored in local timezone. I set webAPI formatters as DateTimeZoneHandling.Local.
The issue is, users can be in different time-zones. when they receive the dates, there are in the server's timezone.
On the client, whatever changes they do in dates, the Angular transformers change the dates to UTC before sending that to server. This time, server changes that back to it's local time and saves back to the database.
This time, the date stored is not what user's actually selected but the local date of the server and is causing inconsistency in the data.
I want this conversion to preserve the datetime at both the ends. Any suggestion?

Related

Need to convert UTC date time into local timezone in SQL Server

We are currently doing an upgrade on Oracle source, because of which all the date time field timezone will change to UTC.
Currently, we have SSIS packages in place for moving data from Oracle to SQL Server Database.
Is there any way we could have a shortcut to change the dates to local timzezone before moving them in our database in SQL Server? Rather than updating each Package/Procedure to include the timezone change. If its a date field coming up, change the timezone from UTC to local time before adding it to the Database in SQL Server.

In SSMS, display DateTimeOffset value in local timezone

Isn't there a way to configure Sql Server Management Studio to, in query results, display the value of a DateTimeOffset column in the client's local timezone, instead of UTC?
Example: I'm in timezone +02:00. A value is stored as 2016-07-27 22:00:00.0000000 +00:00. Currently, this value is displayed as such in query results. I want it to be displayed (formatted) as 2016-07-28 00:00:00.0000000 +02:00 when executing this query on SSMS on my machine.
Currently I'm manually using something like SWITCHOFFSET(CONVERT(datetimeoffset, <DateColumn>), DATENAME(TzOffset, SYSDATETIMEOFFSET())) which is obviously very cumbersome.
If I'm not mistaken, in Oracle the NLS parameters on session level could be used for this. Is there something similar in Sql Server?
Neither SQL Server nor SSMS have any ability to work with the concept of a "session time zone" (like Oracle, MySql, etc. do).
Also, it wouldn't make sense to automatically convert the offset anyway, as the offset is actually part of the stored datetimeoffset value.
If you're using SQL Server 2016, or Azure SQL Database, you can use the new AT TIME ZONE statement, as in:
SELECT yourdto AT TIME ZONE #thetimezoneid
Otherwise, consider using my SQL Server Time Zone Support project.
Neither of which can give you the system's time zone ID. You must know which time zone you want to work with.

How to change SQL Server date

We have just put an application into production which is hosted on AWS using their SQL Server RDS.
We have discovered that the database is set to UTC time, where our local development database isn't.
For example. I'm trying to insert a date of 2016-06-27 08:00. This works fine on our local server, but on AWS, the date is set to 2016-06-26 22:00
I'm trying to replicate the problem on our local server, and I changed the server's timezone to UTC, but the date gets inserted as 2016-06-27 08:00, but I should now expect it to be inserted as 2016-06-26 22:00.
Is there some SQL Server setting I need to change to have a date automatically converted to insert UTC?
The Sql Server DateTime datatype doesn't know anything about timezone. The time inserted is determined by the application posting the time. So this issue is going to be in your application, not in the database.
My recommendation would be to either:
Store all DateTime's as UTC converting them in the application before saving
Use the DateTimeOffset type which stores the timezone information as well as the time

Azure search service datetime conversion

I'm started using azure search service and i have roadblock for now.
I have modified-by column in my native DB table, which is actually part of my retrieve data from azure search. Native DB column always has date time in UTC format now when we retrieve BL layer does the conversion based on User Locale.
Do we have anyway to convert UTC to locale date in azure search as my search is independent and not passing through my BL layer to avoid slow down in search.
Azure Search accepts date/time values with full time zone information (Edm.DateTimeOffset -- e.g. 2012-12-03T07:16:23-07:00) and then normalizes them to UTC for storage purposes. Azure Search itself will not convert date/times to different time zones for you. If you need to convert for the locale of each user on a per-search basis, then you will need to do the conversion on the client side.
If your search client is a browser, then maybe this solution will work for you: Convert UTC date time to local date time using JavaScript

Best Practices working with Datetimeoffset

I already asked a similar question but I didn't get the answer I want. (It's my mistake!)
I have a website with a SQLServer 2008R2 database located in one country (US for example).
Many clients from different countries access to this website. All datetime datatypes are datetimeoffset. The datetime are saved using sql server datetime in US.
What should I do to convert the dates according to the client datetime zone and do I need to save the time zone different in a table to be able to convert the datetime from the database to the client?
I would store the data as UTC (and in fact I mandate that our servers are all set to UTC time with no DST adjustments - not only does this prevent gaps and overlaps twice a year, but it also makes job scheduling etc. much more predictable and reliable).
You can easily convert UTC data to any other time zone. You can use a calendar table to get the offset correctly and to account for things like DST. See how to convert all datetime columns in a sql server 2005 express database with data to UTC, Where to set a UTC datetime value in n-tier application: Presentation Layer, Domain, or Database?, How do I handle the timezones for every Chat message and http://web.archive.org/web/20070611150639/http://sqlserver2000.databases.aspfaq.com/why-should-i-consider-using-an-auxiliary-calendar-table.html
Important: DATETIMEOFFSET is not DST-aware!

Resources