how to set local time based on time zone in asp.net - 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.

Related

Firebase Realtime Database, synchronized with Google sheet, accepts wrong Date from Datepicker sheet cell

How Google Sheet looks
How Firebase Database looks
So the date keeps saving as the day before date;
At Google script appsscript.json is set my real timezone:
"timeZone": "Europe/Kiev"
Any suggestions?
It sounds like the two timestamp values actually refer to the same moment in time. Firebase timestamps are always in UTC.
2021-05-16 00:00 GMT+3 refers to the same point in time as 2021-05-15 21:00 GMT+0. The timezone difference between Kiev and GMT is two hours + one hour because of daylight saving time, so during EEST the two serialized values will look like they are three hours apart.
You can deal with the timezone difference in the client, displaying the timestamp value you retrieve from Firebase in the local time of the client. If the client is in the Kiev timezone, the value will then look the same as it was in the Google Sheet originally.
If you want to save the date as a static text string instead of as a Date object, you may want to convert it in Apps Script with const timezone = SpreadsheetApp.getActive().getSpreadsheetTimeZone() and const dateString = Utilities.formatDate(myDate, timezone, "yyyy-MM-dd"), or in JavaScript with the Intl object or a library like moment.js.

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.

User-friendly time zone names in Symfony2

When Symfony2 renders a form input of type "timezone", it uses the names from the time zone database (e.g. "America/Los Angeles") for both the option values and the display.
Is there a way to display a more user-friendly name for each zone (for users who don't know which Olson/IANA time zone they're in), e.g. the UTC offset and a list of major cities in that time zone?

asp.net now() function with different time zones

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

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