asp.net now() function with different time zones - asp.net

I will be entering some values into a database based on input from users. One value will be the date and time of the input.
I'm not sure how this works at all, but a user from e.g. New York enters some data and the website is hosted in the UK...what time will be stored?
Up until now I had been using the "now()" function to record the date and time, but just occured to me that this could happen! What else should I use?
Thanks,

UTC, works in any time zone, you can either save all dates in your storage as UTC or use proper type that includes time zone data, for example in SQL 2008 you can use datetimeoffset

Related

Date and Time stored in the server is different from the time in the client side

I am developing an application using ReactJS and ASP.NET. Here I need to display the time a record is updated, the time different between now and the last updated time.
Once it is in stage server, as the time in stage and local is different, it gives me an incorrect time as it is comparing the time in the server.
I am using DateTime.UTCNow in the server to store the updated time.
The c# DateTime.UtcNow gives a snapshot date and time as it would be in Coordinated Universal Time. When this date is passed to the browser to be used by JavaScript it is usually represented by a string in the ISO 8601 format which looks like this "2007-03-01T13:00:00Z".
Then in the browser it can will be converted to the Date type and displayed like so.
var date = new Date("2007-03-01T13:00:00Z");
var dateTextInLocalTime = date.date.toLocaleString();
It is normal that the dateTextInLocalTime be shifted in comparison to the Utc counterpart seen on the server since it is converted to your browsers local time.

Get user local time

The date/time data in the database is stored as Oracle Time Stamp with Time Zone (02-FEB-2013 13:25:00 US/PACIFIC).
When I read the data I need to determine the difference between this time and user's current local time. Using above, I can get user's local time offset:
OracleTimeStampTZ dtzLastActivity = new OracleTimeStampTZ(dr["LAST_ACTIVITY_TZ"].ToString());
TimeSpan tsOffset = dtzLastActivity.GetTimeZoneOffset();
Can I get the local time, maybe using TimeZoneInfo, knowing only the offset, so I can then subtract the time in database from local time to get the difference?
US/Pacific is an IANA/Olson time zone identifier - not a Windows time zone id. So you can't use the TimeZoneInfo classes for it.
Instead, use a TZDB implementation, such as NodaTime. It has full awareness of these types of time zones, and can do the type of calculation you are looking for.
BTW - US/Pacific as actually an alias for America/Los_Angeles. You can find a list of these time zones here.
UPDATE
Sorry, I overlooked that you are using OracleTimeStampTZ. This appears to already implement the TZDB, so you should be able to use it directly. Try the following:
var span = DateTime.UtcNow - dtzLastActivity.ToUniversalTime().Value;

asp.net + Testing timezone

I am try storing my date values in UTC format into my SQL Server DB and then convert them to local time for displaying, seem to work fine, I can see that my DB date values stored are different(I presume it's converted to UTC already), retrieving it and display is also accurate until I try to test a different timezone by changing it in the Date and Time option(right bottom of Windows time settings in the task bar) so to "migrate" myself. Apparently, the dates still remain as I am in my own country even though the timezone which I changed to has a 3 hrs difference.
Can somehow please advice on a way to test the date display on a different timezone?
Thanks.
Storing date values to SQL Server:
DateTime dateFrom = DateTime.Parse(startDateTime).ToUniversalTime();
DateTime dateTo = DateTime.Parse(endDateTime).ToUniversalTime();
parameters.Add(new SqlParameter("#StartDateTime", dateFrom));
parameters.Add(new SqlParameter("#EndDateTime", dateTo));
Retrive from DB and Display:
DateTime date = DateTime.UtcNow.ToLocalTime();
date = DateTime.Parse(dr["StartDateTime"].ToString()).ToLocalTime();
litDateTimeFrom.Text = date.ToString("dd MMM yy hh:mm tt");
date = DateTime.Parse(dr["EndDateTime"].ToString()).ToLocalTime();
litDateTimeTo.Text = date.ToString("dd MMM yy hh:mm tt");
First thing, I am assuming that you have changed time zone information on the server machine (because that is what will be used in ToLocalTime method). If not then that's what you need to do first for testing.
Secondly, have you tried restarting IIS (or your web application) after changing the system time zone? That is necessary because time-zone information could have been cached within .NET framework (in TimeZoneInfo class).
Said all that, to me, this does not make sense. Typically, time-zone information need to flow from user (client) machine because you want to show user local time (as relevant to his time-zone). So it would mean that you need to figure out the current user's time zone based on a) culture info from browser or b) what is stored in his profile data (perhaps country or actual time-zone) c) similar scheme. Once, user time-zone is known, you can use
TimeZoneInfo class to convert UTC time to corresponding local time.

how to set local time based on time zone in asp.net

Hi i am developing a website where user will select the timezone from a dropdown. And in my database the datetime are stored in UTC format. How i can display his profile all other things as per timezone selected by user.
You can store the timezone offset for the user and apply that to the UTC time each time you want show the date/time.

how to store datetime as per internationalization

I am having a project which will have country dropdown at the top to select countries. For example USA, India and Hong Kong. Depends on the country selection several features will be enabled or disabled.
In project I am having base country by default for example India. So whenever any user will come. I want to store its login datetime as per Indian standard time regardless user is from Hong Kong, USA or India. My webserver is hosted in USA.
Is that possible?
What you want to do is store any datetime value as UTC. Which means storing as a time zone independent value.
You can either set some sort of insert trigger in your MS-SQL server (I assume you use MS-SQL because of the asp.net part). There you determine the UTC time like this.
`-- MS SQL (use instead of GETDATE())
DECLARE #currentTime datetime
SET #currentTime = GETUTCDATE()`
If you want to do it in code just use UtcTimeNow to access the UTC time.
// C# (use instead of DateTime.Now)
DateTime currentTime = DateTime.UtcNow;
If you want you can always convert the UTC time to any given local time. By using the TimeZoneInfo class.
That way you have the right time in your database (UTC) and can convert it to the appropriate time for the User.
The most important part when dealing with times and timezones is to store it as UTC. That way it's can be transported and compared easily.
If you save your time in unixtime, you can have a field in the user table called offset. Where it's value is how much the system needs to offset the time.
Then you'd just fetch that variable and save that as a session value or something similar. And use that to offset the datetime in the system.
For example. Hong Kong is +8 hours from GMT. Now in GMT Unixtime is: 1242293400 (9:30 am). 8 hours is 28800 seconds. You would then store this value in your server. So the time would be 1242322200 (5:30 pm same day)
IF you are using SQL Server 2008, look at the new datetime datatypes which allow you to store the offset/timezone as well.

Resources